digitalmars.D.learn - delegete with foreach
- %u (38/38) Jan 11 2007 Hello,
- Lutger (4/17) Jan 11 2007 You are correct, though it is not a function but a delegate. The name of...
- Frits van Bommel (5/15) Jan 11 2007 Delegate literals are also known as "anonymous delegates". They don't
- Lutger (6/10) Jan 11 2007 Oops I misunderstood the question, Frits van Bommel is right of course.
- Frits van Bommel (11/25) Jan 11 2007 In fact, you can do that even better with function literals.
Hello, i try to understand this programm. import std.stdio; void main() { auto dg = delegate (int delegate( inout int) dgp) { static int j=0; int res=0; while( j < 5){ j++; res= dgp(j); if(res) break; } return res; }; foreach (int i; dg) { writefln(i); } } Output: 1 2 3 4 5 Is this a function with a return value and with one delegate parameter ? auto dg = delegate (int delegate( inout int) dgp) { . . . return res; } If this a function, what name have the function? Thanks
Jan 11 2007
%u wrote:Is this a function with a return value and with one delegate parameter ? auto dg = delegate (int delegate( inout int) dgp) { . . . return res; } If this a function, what name have the function? ThanksYou are correct, though it is not a function but a delegate. The name of the delegate is 'dg', and it takes as argument 'int delegate(inout int)' which is also a delegate. Return type is infered to be typeof(res).
Jan 11 2007
%u wrote:Is this a function with a return value and with one delegate parameter ? auto dg = delegate (int delegate( inout int) dgp) { . . . return res; }It's a delegate literal with one delegate parameter.If this a function, what name have the function?Delegate literals are also known as "anonymous delegates". They don't have names :). See http://www.digitalmars.com/d/expression.html#FunctionLiteral
Jan 11 2007
Frits van Bommel wrote:Delegate literals are also known as "anonymous delegates". They don't have names :). See http://www.digitalmars.com/d/expression.html#FunctionLiteralOops I misunderstood the question, Frits van Bommel is right of course. You can toss dg around like a regular variable, it doesn't really have a name. auto whatever = dg; You can do the same with function literals.
Jan 11 2007
Lutger wrote:Frits van Bommel wrote:In fact, you can do that even better with function literals. Delegate literals contain a pointer to the current stack-frame, function literals don't. That means function literals can safely be called after the enclosing function returns (and can thus safely be returned from said function, for instance). Though in practice any delegate literal that would also be a valid function literal (at that location in the code) if you added 'function' to the declaration (replacing 'delegate' if present) will probably work, since that pretty much means it doesn't actually *use* the pointer to the stack frame...Delegate literals are also known as "anonymous delegates". They don't have names :). See http://www.digitalmars.com/d/expression.html#FunctionLiteralOops I misunderstood the question, Frits van Bommel is right of course. You can toss dg around like a regular variable, it doesn't really have a name. auto whatever = dg; You can do the same with function literals.
Jan 11 2007