Thread overview
delegete with foreach
Jan 11, 2007
%u
Jan 11, 2007
Lutger
Jan 11, 2007
Frits van Bommel
Jan 11, 2007
Lutger
Jan 11, 2007
Frits van Bommel
January 11, 2007
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
January 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?
> 
> 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).
January 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
January 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#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.
January 11, 2007
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...