delegates, functions, and literals confusion
Mike Parker
aldacron at gmail.com
Thu Jul 4 09:09:22 PDT 2013
On Thursday, 4 July 2013 at 13:23:23 UTC, CJS wrote:
> Thanks for the detailed answer!
>
> Just to clarify: So if f is an inner function then &f will be a
> delegate even if it doesn't reference anything in the
> environment in which it was defined? (i.e. even if it could
> have been typed as a function?)
That is correct. Unless the inner function is static. I had
actually forgotten about this until I just looked it up in the
docs[1]. Taking the address of a static inner function will give
you a function pointer rather than a delegate.
void foo() {
static int f() { return 10; }
int g() { return 20; }
iTakeAFuncPtr( &f );
iTakeADelegate( &g );
}
In which case you can modify static local variables:
import std.stdio;
void foo( int function( int ) fp ) {
writeln( fp( 5 ));
}
void main() {
static int k = 10;
static int mult( int i ) { return i*10; }
foo( &mult );
}
[1] http://dlang.org/function.html
More information about the Digitalmars-d-learn
mailing list