aardappel / lobster

The Lobster Programming Language
http://strlen.com/lobster
2.24k stars 121 forks source link

Passing too many arguments to an anonymous function doesn't yield compile time error #229

Closed MoonKraken closed 1 year ago

MoonKraken commented 1 year ago
let add = fn(a, b):
    a + b

print add(1,2)
print add(1,2,3)

Compiles and runs without a problem. The third argument passed to add appears to be ignored.

aardappel commented 1 year ago

That is a feature :)

Anonymous functions are used very frequently to implement the "body" of a custom control structure, and we want the higher order function to be able to pass all useful values, and the function value to pick the ones needed.

If you look at the implementation of map, it passes both the element and its index. Many users of this function do not use the index. This makes map function identical to for which also does this.

It is almost similar to multiple return values. Sender sends all useful stuff, receiver picks what they need.

MoonKraken commented 1 year ago

Interesting, apologies for the false alarm! OOC are there any other mainstream languages that take this approach? I think JS is one, just wondering if there are any that are statically typed. In the scenarios you mention, I think many would require you to specify _ or something similar for the parameter that you don't need.

aardappel commented 1 year ago

Yeah I think within statically typed languages this is pretty specific to Lobster, since it has such a heavy focus on HOFs/lambdas. It made sense to me at the time :)

I wouldn't want to write map(xs) x: x + 1 as map(xs) x, _: x + 1 all the time.. besides there's map(xs): _ + 1 which makes that even further impossible. Or have to use a specialized map_indexed or whatever.