Anonymous function syntax

Robert Jacques sandford at jhu.edu
Fri Sep 23 20:10:55 PDT 2011


On Fri, 23 Sep 2011 03:03:04 -0400, Alex_Dovhal <alex_dovhal at yahoo.com> wrote:

> "Robert Jacques" <sandford at jhu.edu> wrote
>> What I worry about though is variable hijacking rules. e.g.
>>
>> auto b = 5;
>> reduce!\a+b(map!\a+b([1,2,3,4));
>
> Nice.
>
> What if we extend your proposal to
> \(comma_separated_params)simple_expression
> \(comma_separated_params){expressions}
>
> Then no variable hijacking would be
>  auto b = 5;
>  reduce!\(a)a+b(map!\(x)x+b([1,2,3,4));
>  reduce!\a+b(map!\a+b([1,2,3,4)); //error, shadowing b

Well, actually, this is Jason's proposal and it's explicitly for a std.algorithm style, by convention, super-short lambda syntax. i.e. primarily for one liners and std.algorithm. If you go the comma_separated_params, then I think the a => a+x; route is better.

Actually, I made a error in my final example as each lambda must be terminated somehow. i.e. with a semi-colon:

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

Also, you could consider !\ to be a bit redundent and allow 'symbol\statement' to be lowered to 'symbol!\statement'. Thus the following would also be valid:


reduce\a+b;(map\a+b;([1,2,3,4));

As for hijacking, I think by-convention lambdas should be disallowed if a or b are used in the lambda and are already defined in the namespace. In those cases, you'd have to revert to a more general syntax, i.e.

reduce!"a+b"(map!(a => a+b)([1,2,3,4)));


More information about the Digitalmars-d mailing list