On 2010-12-14 22:04, Nick Sabalausky wrote:
"Jacob Carlborg"<d...@me.com> wrote in message
news:ie8f5f$o6...@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?).
I guess for anything more complicated you would have to use the =>
syntax. BTW, I just verified that this works:
Array(1, 2, 3, 4, 5).sortWith(_ > _)
You can try it out for yourself at: http://www.simplyscala.com/
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.
I would also love to have that syntax. I also love the syntax I
previously suggested in this thread, there are many to choose of.
--
/Jacob Carlborg