Carpet

Carpet

2M Downloads

Scarpet functions are not first-class although the documentation says they are

fghsgh opened this issue ยท 1 comments

commented

The documentation says:

Functions can also be assigned to variables, passed as arguments [...]

...which implies that functions are first-class values. However, the following are not possible in Scarpet:

a() -> 'test'; // function which returns 'test'
b = a; // should assign the function a to b, should make b() do the same as a() aka return 'test'
print(b()) // should print 'test', instead complains about function b not being defined

In this case, it seems that a doesn't refer to the function a, as it should.

print((_() -> 'test')()) // should make a nameless function which returns 'test', call it, and print the result, instead complains about missing parameters for operator *

In this case, it seems to interpret the syntax (...)() as a multiplication instead of as a function call.

a() -> print('test'); // creates function a which prints 'test'
schedule(0,a); // should schedule the function a to run at the end of the tick, instead complains about function 0 not being defined

In this case, a doesn't refer to the function a but rather to a nonexistent variable a, which is converted to the string '0' and then searched for as a function. The function 0 of course doesn't exist, which is where the error message comes from.

However, making functions simple first-class values creates the issue that functions need to be prefixed with global_ or outscope()'d to allow calling them from within other functions. More complicated scoping rules will be necessary, or the documentation should be changed. Preferably the former, as it gives the language more capabilities.

commented

a is a variable referring to variable a
a() is a call to function a. In scarpet these sets are separate.
You can grab a reference to a method by assigning it to a value (anonymous, or not)
foo = ( bar() -> print('baz') );
then call it
call(foo)
or
bar()
these are the same