nitlang / nit

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

Callref syntax #2775

Closed privat closed 5 years ago

privat commented 5 years ago

Note: I forgot to independently PR the syntax for callrefs (used by #2774), so here it is.

Introduce the & syntax to capture and reference calls. This is used to provide a simple function pointer construction that will be used later to implement full lambdas. However, this is not expected to be used by lambda programmers :)

var ref = &recv.foo

Currently, the syntax is analogous to a simple call (recv.foo) with a prefix &.

On chains, only the last call is captured (. have a higer precedence than &)

var r = &foo.bar.baz
# is equivalent with
var x = foo.bar
var r = &x.baz

Since the syntax is analogous to a call (except the &), there is always a receiver (including the implicit self or sys) and arguments are accepted by the parser.

var r = &foo
# is equivalent with
var r = &self.foo

There is no clear syntax proposal to avoid the capture of a receiver since a receiver is statically expected to resolve the method name.