Let’s say that I stored a reference to a method in a variable. Then, I pass the method reference elsewhere, so that I can no longer access the original instance that the method came from. Can I use the method reference to get that original instance (without also passing the original instance as a separate argument)?
Code to demonstrate what I mean:
class Main {
static function main() {
var obj = new MyClass();
var func = obj.doSomething;
passFunc(func);
}
static function passFunc(func:()->Void):Void {
// how to get the value of obj here (without also passing obj)?
}
}
private class MyClass {
public function new() {}
public function doSomething():Void {
trace("did something");
}
}
I’m particularly interested in doing this with the cpp target, and it’s okay if a solution doesn’t work on other targets. Thanks!
Unfortunately, I can’t call the function before I need a reference to the instance. I also can’t change the function signature, even if I could call it. Thanks, though.
Because I’m “helpful”, and curious about this myself I asked Github Copilot about it. It didn’t really have a solution other than passing the instance in addition to the function. e.g. passFunc(func,obj);
Since I can’t really change the function signatures to pass obj around, I’m basically hoping that there’s some kind of low-level reflection API that I’m not aware of (even if it is specific to hxcpp and I need to conditionally compile it to hide it from other targets).
For the cpp target only you can make it work with this:
class Main {
static function main() {
var obj = new MyClass();
var func = obj.doSomething;
passFunc(func);
}
static function passFunc(func:()->Void):Void {
var obj:MyClass = untyped __cpp__("(hx::Object*)func->__GetHandle()");
obj.doOther();
func();
}
}
private class MyClass {
public function new() {}
public function doSomething():Void {
trace("did something");
}
public function doOther():Void {
trace("did other");
}
}