Delegate function access to classes local variable
Timon Gehr
timon.gehr at gmx.ch
Sun Nov 10 13:09:23 PST 2013
On 11/10/2013 09:03 PM, Colin Grogan wrote:
> For example,
> Column!(int, int) randonNumberColumn = new Column!(int, int)((ref
> m)=>to!string(uniform(m.vars[0], m.vars[1])), 1, 10);
>
> will work.
> However,
> Column!(int, int) incrementalNumberColumn = new Column!(int,
> int)((ref m)=>{m.vars[0]+=m.vars[1]; return
> to!string(m.vars[0]-m.vars[1]);}, 1,2);
>
> wont.
>
> Maybe my syntax is just wrong or this is simply a limitation?
>
This will work:
Column!(int, int) incrementalNumberColumn = new Column!(int, int)((ref
m){m.vars[0]+=m.vars[1];return to!string(m.vars[0]-m.vars[1]);}, 1,2);
> Also, if you could explain what the => operator is doing there that
> would be great. I couldnt find the info on it in the docs...
(ref m)=>exp
is the same as
(ref m){ return exp; }
http://dlang.org/expression.html#Lambda
(hence
(ref m)=>{return exp; }
which is the same as
(ref m)=>(){ return exp; }
is the same as
(ref m)=>()=>exp
the return type of this expression is 'string delegate()' instead of
string and therefore the compiler rejects your code.)
More information about the Digitalmars-d-learn
mailing list