英文:
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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论