Haxe 4..x support for inlining delegate calls

53 Views Asked by At

In the recent HaxeUp talk somebody mentioned inline delegates support in Haxe 4.x and I am looking for an example of how this works.

I would expect something like this (this does not compile):

    static function performOperation(a: Int, b: Int, operation: (Int, Int) -> Int): Int {
        return inline operation(a, b);
    }

    static inline function multiply(a: Int, b: Int): Int {
        return a * b;
    }

to result in a single function call (performOperation) while multiply would be inlined.

1

There are 1 best solutions below

0
Alexander Gordeyko On BEST ANSWER

You can do this and it will work:

class Test {

static function main() {
    trace(test(2, 3));
}
  
static function test(a, b) {
    return performOperation(a, b, multiply);
}
  
static inline function performOperation(a:Int, b:Int, operation:(Int, Int) -> Int):Int {
    return operation(a, b);
}

static inline function multiply(a:Int, b:Int):Int {
        return a * b;
}

}

JS output:

class Test {
static main() {
    console.log("Test.hx:3:",Test.test(2,3));
}
static test(a,b) {
    return a * b;
}
}