delegete with foreach

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Thu Jan 11 09:41:37 PST 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...


More information about the Digitalmars-d-learn mailing list