Help me /prog/, i'm making a language and i can't decide on what syntax to use for typed lambdas
Name:
Anonymous2012-06-10 17:16
Perl 6's is nice:
@array.map: -> Int $a { $a + 1 }
# or
@array.map: { $^a + 1 }
# method calls are simpler:
@array.map: { .say }
You'll rarely ever see $_ in 6. Type annotations are optional because the language is gradually typed.
Rust has done nearly the same thing. If a closure it the final argument to a call, you can put it outside the arg list:
// vec.each is a method call on vec,
// { ... } is a supplied closure
for vec.each { |e|
// operations on e
}
Type annotations are mostly optional. It is statically typed, but with inference.
Inference or no, I like both of these forms. I think I prefer Perl 6's, just because the placement of the declaration is better, and the colon makes what's happening very clear (this is a closure, not a special control block. Though in Rust it's *both* in a very fundamental way, thanks to 'for'.)
Whether the type comes first or second is a matter of taste. If you have qualifiers that may inform your decision.