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

solidstate1991 laszloszeremi at outlook.com
Fri Sep 30 21:11:48 UTC 2022


Let's say I have a class or an interface with a function, and I 
want to pass that to a template to generate another function (in 
this case one that passes it to a scripting engine, and allows it 
to be called from there).

Currently I have the following issues:

1. I have to pass the class/interface type to the template, and I 
cannot find anything about how to solve this issue.
2. It also doesn't work at the moment, as it wants to call the 
member `Func` (name of the template argument) rather than the 
Function described as such. (There's also probably in 
std.functional I don't know about)

Code:

```d
extern (C) public int registerDDelegate(alias Func, 
ClassType)(lua_State* state) nothrow
		if(isSomeFunction!(Func)) {
	import std.traits:Parameters, ReturnType;
	
	Parameters!Func params;
	int stackCounter = 0;
	stackCounter--;
	ClassType c = luaGetFromIndex!ClassType(state, stackCounter);
	try {
		foreach_reverse(ref param; params) {
			stackCounter--;
			param = luaGetFromIndex!(typeof(param))(state, stackCounter);
		}
	} catch (Exception e) {
		luaL_error(state, "Argument type mismatch with D functions!");
	}
	
	try {
		static if(is(ReturnType!Func == void)) {
			c.Func(params);
			return 0;
		} else static if(is(ReturnType!Func == struct)) {
			auto retVal = c.Func(params);
			static foreach (key ; retVal.tupleof) {
				LuaVar(key).pushToLuaState(state);
			}
			return cast(int)retVal.tupleof.length;
		} else {
			LuaVar(c.Func(params)).pushToLuaState(state);
			return 1;
		}
	} catch (Exception e) {
		//luaPushVar(L, null);
		lastLuaToDException = e;
		try {
			luaL_error(state, ("A D function threw: 
"~e.toString~"!\0").ptr);
		} catch(Exception e) {
			luaL_error(state, "D threw when stringifying exception!");
		}
		return 1;
	}
}
```


More information about the Digitalmars-d-learn mailing list