How can I get the "owner" of a method?

Ali Çehreli acehreli at yahoo.com
Sat Oct 1 01:18:38 UTC 2022


On 9/30/22 14:11, solidstate1991 wrote:

 > extern (C) public int registerDDelegate(alias Func,

That can be a lambda that takes ClassType. I wrote the following along 
with place holder as I assumed them to be:

class C {
     string s;

     void foo() {
         import std.stdio : writeln;
         writeln("Look ma: It was ", s, " but I am using it in D!");
     }
}

auto luaGetFromIndex(C)() {
     auto c = new C();
     c.s = "created in Lua";
     return c;
}

extern (C)
int registerDDelegate(alias Func)() {
     import std.traits : Parameters;
     alias ClassType = Parameters!Func[0];
     auto c = luaGetFromIndex!ClassType();

     Func(c);
     return 0;
}

void main() {
     // Here: The lambda takes a C:
     registerDDelegate!((C c) => c.foo);
}

Ali




More information about the Digitalmars-d-learn mailing list