Given only a reference to a method, can I get the instance the method belongs to in Haxe?

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!

Hej,

If I understand correctly, and since your method doSomething returns Void here, you could do that it returns this :

public function doSomething():MyClass {
	trace("did something");
	return this;
}

But maybe there is something special that could be used in cpp target, idk.

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);

I guess that ignores what you said about changing the function signature.

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");
	}
}
1 Like

Thank you, that’s perfect!