www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - delegete with foreach

reply %u <not possible.de> writes:
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
next sibling parent Lutger <lutger.blijdestijn gmail.com> writes:
%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?
 
 Thanks
You 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
prev sibling parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
%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
parent reply Lutger <lutger.blijdestijn gmail.com> writes:
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#FunctionLiteral
Oops 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
parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Lutger wrote:
 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#FunctionLiteral
Oops 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.
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...
Jan 11 2007