Stream: beginners

Topic: How will roc handle functions without arguments?


view this post on Zulip Niclas Overby (Feb 23 2023 at 09:21):

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 {}

view this post on Zulip Martin Stewart (Feb 23 2023 at 09:49):

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

view this post on Zulip Brian Carroll (Feb 23 2023 at 10:53):

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!

view this post on Zulip Joshua Warner (Feb 23 2023 at 16:42):

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.

view this post on Zulip Martin Stewart (Feb 23 2023 at 16:47):

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

?

view this post on Zulip Ayaz Hafiz (Feb 23 2023 at 16:49):

the rule is

so 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

view this post on Zulip Ayaz Hafiz (Feb 23 2023 at 16:49):

here's the motivation

view this post on Zulip Ayaz Hafiz (Feb 23 2023 at 16:50):

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