nitlang / nit

Nit language
http://nitlanguage.org
Apache License 2.0
239 stars 65 forks source link

Proposal for lambda expression syntax #2758

Closed privat closed 5 years ago

privat commented 5 years ago

This extends the Nit syntax with lambdas expressions.

Lambdas are basically anonymous functions, so just accept functions without a name as expressions.

var x = fun(i: Int): Int do
    return i + 1
end
# or on a single line
var x = fun(i: Int): Int do return i + 1 end

fun, do and end are mandatory, the rest is optional

var x = fun do end

The main weirdness is the mandatory end that makes the following invalid

# invalid:
var x = (fun(i: Int): Int do return i + 1) # missing `end`, unexpected `)`

There is no semantic yet, nor implementation, this might come in some future PR

Morriar commented 5 years ago

No way to define it without the final end?

privat commented 5 years ago

The current grammar broke really badly if end is missing. I think that sablecc4 is required.