在 Haxe 中传递任意函数参数列表 [英] Passing list of arbitrary function arguments in Haxe

查看:26
本文介绍了在 Haxe 中传递任意函数参数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 ActionScript 中,我可以在函数声明中使用 ... 以便它接受任意参数:

In ActionScript I can use ... in a function declaration so it accepts arbitrary arguments:

function foo(... args):void { trace(args.length); }

然后我可以调用传递数组的函数:

I can then call the function passing an array:

foo.apply(this, argsArray);

我想用未知类型和计数的参数调用函数.这在 Haxe 中可行吗?

I'd like to call the function with arguments of unknown type and count. Is this possible in Haxe?

推荐答案

从 Haxe 4.2 开始,Haxe 将原生支持其余参数:

Starting with Haxe 4.2, Haxe will have native support for rest arguments:

function f(...args:Int) {
    for (arg in args) {
        trace(arg);
    }
}

f(1, 2, 3);

...args:Int 只是 rest:haxe.Rest 的语法糖.只有函数的最后一个参数可以是剩余参数.

...args:Int is simply syntax sugar for rest:haxe.Rest<Int>. Only the last argument of a function can be a rest argument.

您也可以使用 ... 来传播"调用带有 rest 参数的函数时的数组:

You can also use ... to "spread" an array when calling a function with a rest argument:

final array = [1, 2, 3];
f(...array);

这篇关于在 Haxe 中传递任意函数参数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆