Function types becoming dynamic after compilation

Hi All,

Went with Ilir-Liburn’s suggestion in the end and built a macro… The library can be found here: haxe-delegates (0.0.0)

I had actually been musing over this idea for a year or so before getting it in, and I’m happy to say it works. For the Hxcpp target, the delegates appear to perform 40% faster than function types, and 200% faster when inlined. It would be nice to see how it performs on other targets.

You can use it by going:

var delegate : Delegate<(Int, Int)->Int> = DelegateBuilder.from(myFunction);

public function myFunction(a : Int, b : Int) : Int {
    return a + b;
}

delegate.call(4, 3);

Essentially, all the macro does is wrap function pointers and calls into an object. It had some serendipitous side effects, such as type-strictness. The compiler will recognise if you’re trying to pass Null<Int> where it expects an Int and it will complain, thereby putting the onus back on the developer.

It also handles inlining quite nicely, as delegate objects are given private access to the class that implements them via a _parent variable, so it has a view onto any variables declared externally to itself.

Obviously as a first release, there’s still limited functionality, but this is a proof of concept that such a thing is possible and possibly even preferred in some cases. I may be able to circumvent excessive object creation using pooling and weak-references, and would like to get full support for local functions in at some point.

Kind regards.

1 Like