Lambda syntax, etc

bearophile bearophileHUGS at lycos.com
Mon Feb 9 06:42:37 PST 2009


grauzone Wrote:
> I really like this one, but I'd prefer something like
>  > auto r = find(range, {a -> a.Weight > 100});
> 
> Note the -> instead of the =>, to avoid any ambiguities with the 
> comparison operator.

Let's play more; then what do you think about (all the following are legal):

auto r1 = range.find({ x -> x.weight > 100 });
auto r2 = range.find({ x :: return x.weight > 100; });
auto r3 = range.find({ x :: stmt1(x); stmt2; });
auto r4 = range.find({ x, y :: stmt1; return foo(y); });

I like those enough, they seem balanced, uniform, not too much error-prone, they have only one visual chunk, short enough and easy to write :-)

Note that this syntax:
auto r1 = range.find({ x -> x.weight > 100 });
using my dlibs is equivalent to the following:
auto r1 = range.find((ArrayType1!(typeof(range)) x) { return x.weight > 100; });

Bye,
bearophile



More information about the Digitalmars-d mailing list