Inlining an inner function across classes

Hello there, I’m still a beginner with Haxe, so maybe someone can give me a hint how to solve an inlining design I’ve come across today.

I’m basically having some base class X, that uses some method from a derived class Y. However, calling I’d like to inline the call to that function, so that there is actually no function call at all.

Using standard notation (involving a function call), I want to do this:

abstract class X {
	public function some_f() {
		for (i in 0...1000000) {
			some_g(i);
		}
	}

	abstract function some_g();
}

class Y extends X {
	function some_g(i:Int) {
		// impl of g.
	}
}

I’m not sure how to tackle this with Haxe. I know there are macros, but I haven’t really managed to get my head around them. In C++, I would do something like this for the use case above:

template<typename G>
class X {
	public function some_f() {
		for (i in 0...1000000) {
			G(i);
		}
	}
}

class Y : X<ImplOfGForY> {
}

Any ideas?

I thought about some crazy inversion of control involing in-site inlining, but I couldn’t find any docs/examples.

With haxe 4, you can inline at call site thanks to Support `inline call()` and `inline new C()` expressions by Simn · Pull Request #7425 · HaxeFoundation/haxe · GitHub

1 Like