How will roc handle functions without arguments?
(I'm considering this in the context of generating automated library bindings)
Will this be the only way?
f = \{} -> 1
f {}
You can just write
f = 1
It can be thought of as a function that always returns 1. But if the goal is to prevent the value from being eagerly computed (maybe the value is expensive to compute) then yes you'd write f = \{} -> 1
Yeah, in languages without side effects, the concept of a function without arguments is not as useful as it is in imperative languages. Functions always return the same value for the same arguments. So if it has no arguments, it's the same as a constant!
Actually right now there's a key difference between a function and a constant, in that the function is allowed to be polymorphic but the constant isn't. That leads to the current weird syntax for creating new collections - e.g. myDict = Dict.empty {}
. The {}
is not a dict literal or anything - that's just an empty record, there for the purposes of turning what would have been a zero argument function (aka constant) into a one argument function.
In that respect, a zero-argument function is logically very similar to a function that takes an argument but ignores it.
Joshua Warner said:
Actually right now there's a key difference between a function and a constant, in that the function is allowed to be polymorphic but the constant isn't.
So this means it's not possible to write
a = 1
b : Int64
b = a
c : Float64
c = a
?
the rule is
f = \x -> x
) are allowed to be polymorphic when assigned to a variableso in the case above, that would actually work! but for example, these two wouldn't
a = 1 + 2
b : I64
b = a
c : F64
c = a
a = A
b : [A, B]
b = a
c : [A, C]
c = a
also, the plan is to loosen the restriction in the future. The infrastructure needed to do so just isn't there yet.
Last updated: Jul 06 2025 at 12:14 UTC