On Wednesday, 9 March 2016 at 20:14:13 UTC, Adam D. Ruppe wrote:
---
import std.stdio;
@nogc int delegate(int) dg;
int helper() @nogc {
int a = 50;
struct MyFunctor {
int a;
@nogc this(int a) { this.a = a; }
// the function itself
@nogc int opCall(int b) { return a+b; }
}
// capture a by value
// WARNING: I stack allocated here but
// set it to a global var, this is wrong;
// it should probably be malloc'd, but since
// I know I am just using it here, it is OK
auto myfunc = MyFunctor(a);
dg = &myfunc.opCall;
return dg(10);
}
void main() {
writeln(helper());
}
typeof(&myfunc.opCall) == int delegate(int b) @nogc
what magic is this? I had no idea that taking the address of
opCall would give me a delegate.