delegate bug?
Timon Gehr
timon.gehr at gmx.ch
Fri Nov 9 12:52:05 PST 2012
On 11/09/2012 09:31 PM, Jack Applegame wrote:
> On Friday, 9 November 2012 at 20:22:40 UTC, Timon Gehr wrote:
>> The following works. You can also use a (scoped) delegate in order to
>> allow the caller to close over his context.
>
> Wow! Great! Thanks.
> But I can't understend this syntax:
> x=>x.draw()
>
>
> Where i can read about it?
It is a lambda function literal. It is as if you did
static void action(Figure x){
return x.draw();
}
doAction(figures, &action);
which can also be written in any of the following ways. Because the
types are mandated by the function that is called, you can leave them out:
doAction(figures, function void(Figure x){ return x.draw(); }); //
function literal
doAction(figures, function(x){ return x.draw(); }); // infer types
doAction(figures, (x){ return x.draw(); }); // infer 'function'
doAction(figures, (x)=>x.draw()); // convenient lambda syntax
doAction(figures, x=>x.draw()); // also without parentheses
It is documented here (scroll up a bit, Lambda does not seem to have an
anchor)
http://dlang.org/expression.html#FunctionLiteral
In this case, we used the feature to implement a member function
pointer, but there are many more applications that such a shorthand
syntax makes convenient.
It might generally be helpful for your programming skills to get
accustomed to functional programming a bit. So in case you are
interested, I suggest you consult your search engine of choice about the
subject. You could eg. look into Scheme and/or Haskell and try to
reproduce some of the examples also in D.
More information about the Digitalmars-d-learn
mailing list