Why Ruby?
Nick Sabalausky
a at a.a
Tue Dec 14 13:04:29 PST 2010
"Jacob Carlborg" <doob at me.com> wrote in message
news:ie8f5f$o6e$1 at digitalmars.com...
>
> Probably not, but, for example, Scala allows very compact delegate
> literals:
>
> Array(1, 2, 3, 4, 5).select(_ > 3).collect(_ * _)
>
> Or more verbose:
>
> Array(1, 2, 3, 4, 5).select((x) => x > 3).collect((x, y) => x * y)
>
> I'm not 100% sure I that the syntax is correct.
>
I'd be surprised if the first one is correct, because that "collect(_ * _)"
would seem highly limited (how would you use the same value twice, or use
just the second value, or use them in reverse order?).
The second one reminds me of C#'s lambdas and seems like an excellent
solution to the question of what to do with all the noise of the extra curly
braces, "return" keyword and trailing semicolon. And given that 1. The =>
isn't currently a valid D token anyway, and 2. The (x) and (x,y) are already
being used perfectly fine by the existing delegate literal syntax, I can't
imagine there would be any ambiguity with it in D.
I'd be absolutely in favor of adding that to D. In fact, I'd love to see it
happen. Just require that the part after "=>" is an expression, accept that
the more complex delegates that need statements use the existing syntax
(which should be fine since it's only the single-expression-and-nothing-else
delegates that are seen as verbose in D), and call it a day.
More information about the Digitalmars-d
mailing list