【发布时间】:2015-08-10 12:11:36
【问题描述】:
我实际上是在寻找 JavaScript arguments 功能,但在 Dart 中。
这在 Dart 中可行吗?
【问题讨论】:
标签: dart
我实际上是在寻找 JavaScript arguments 功能,但在 Dart 中。
这在 Dart 中可行吗?
【问题讨论】:
标签: dart
你必须和noSuchMethod一起玩才能做到这一点(见Creating function with variable number of arguments or parameters in Dart)
在班级级别:
class A {
noSuchMethod(Invocation i) {
if (i.isMethod && i.memberName == #myMethod){
print(i.positionalArguments);
}
}
}
main() {
var a = new A();
a.myMethod(1, 2, 3); // no completion and a warning
}
或在字段级别:
typedef dynamic OnCall(List l);
class VarargsFunction extends Function {
OnCall _onCall;
VarargsFunction(this._onCall);
call() => _onCall([]);
noSuchMethod(Invocation invocation) {
final arguments = invocation.positionalArguments;
return _onCall(arguments);
}
}
class A {
final myMethod = new VarargsFunction((arguments) => print(arguments));
}
main() {
var a = new A();
a.myMethod(1, 2, 3);
}
第二个选项允许为myMethod 完成代码并避免警告。
【讨论】: