Haxe 4.x支持内联委托调用。

huangapple go评论131阅读模式
英文:

Haxe 4..x support for inlining delegate calls

问题

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.

我最近在HaxeUp的讲座中听说过Haxe 4.x中支持内联委托,我正在寻找一个示例来了解它是如何工作的。

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.
这段代码会导致一个函数调用(performOperation),而multiply会被内联。

英文:

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

得分: 2

以下是代码的中文翻译部分:

你可以这样做,它将会起作用:

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 输出:

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

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

huangapple
  • 本文由 发表于 2023年5月7日 23:30:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76194808.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定