Anonymous function syntax

Robert Jacques sandford at jhu.edu
Thu Sep 22 21:16:26 PDT 2011


On Thu, 22 Sep 2011 18:59:31 -0400, Jason House <jason.james.house at gmail.com> wrote:

> std.algorithm already introduces a sort of lambda syntax...
> map!"a+b"(...) or map!q{a+b}(...)
>
> If D is looking for its own style of short lambda, maybe implicit use of a and b could be extended.
> Candidates:
> a+b
> {a+b}
> f{a+b}
> auto{a+b}
> auto a+b
>
> Personally, I like the 2nd or 3rd one. The second is visually cleaner while the 3rd is easily parsed and feels very similar to q strings. I picked f because that's frequently used to represent a function. x could work, but that's a common variable name...

I like the idea, although 1,2,3 and 5 are too close to valid D syntax for my comfort. We could borrow from Haskell:

\{a+b}

But I'd prefer not to have special syntax rules inside lambda blocks. (It increases the cognitive load of D) So why not make it a statement? i.e.

\a+b;

then {} would only be for multi-statement or void functions:

\{a+b; return;}

What I worry about though is variable hijacking rules. e.g.

auto b = 5;
reduce!\a+b(map!\a+b([1,2,3,4));


More information about the Digitalmars-d mailing list