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

查看:60
本文介绍了在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将对rest参数提供本地支持:

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< Int> 的语法糖.只有函数的最后一个参数可以是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天全站免登陆