On 2/12/2015 3:40 AM, Freddy wrote:
----
import std.stdio;

auto test1(){
     void testFunc(){
     }
     return &testFunc;
}

auto test2(){
     uint a;
     void testFunc(){
         a=1;
     }
     return &testFunc;
}

void main(){
     writeln(test1()==test1());//true
     writeln(test2()==test2());//false
}
----
Is the intended behavior?

Read the section of the documentation about Delegates, Function Pointers, and Closures [1]. Neither of your pointers are actually function pointers. They are both delegates. The second one, as ketmar said, is a closure because it's a delegate with state. To get a function pointer from an inner function, the inner function must be static. Function pointers can't have state.

```
import std.stdio;

void printAnInt( int function() fptr ) {
    writeln( "From function pointer: ", fptr() );
}

void printAnInt( int delegate() dg ) {
    writeln( "From a delegate: ", dg() );
}

void main() {
    static int foo() { return 1; }
    int bar() { return 2; }

    printAnInt( &foo );
    printAnInt( &bar );
}
```

[1] http://dlang.org/function.html#closures

Reply via email to