Syntax question about inlined functions/delegates/lambdas

bearophile bearophileHUGS at lycos.com
Mon Dec 30 11:16:30 PST 2013


Gordon:

> ...
> case 5. X = ",x); } )();
>         call_function!( (x)         => { writeln("lambda,   
> case 6. X = ",x); } )();
>         call_function!( (x)         =>   writeln("lambda,   
> case 7. X = ",x)    )();
> }

There is also the simpler syntax:

x           =>   writeln("lambda,   case 8. X = ", x)


> So I've learned that syntaxes in cases 2,4,6 are wrong, but 
> they still compile.
> May question is - what do they do? what usage do they have 
> (since they do not trigger a compilation warning)?


Observe:

void main() {
     import std.stdio;

     auto f = (int x) => { x.writeln; };
     f(10)();
}


It prints 10.

The syntax for lambdas is "... => ...", while (...){...} was the 
older syntax. If you use both, you are creating a lambda that 
returns a lambda.

Bye,
bearopile


More information about the Digitalmars-d mailing list