获取传递给 Dart function/constructor 调用的参数集合
Get a collection of arguments passed to a Dart function/constructor call
我主要是在寻找 JavaScript arguments
功能,但在 Dart 中。
这在 Dart 中可行吗?
你必须玩 noSuchMethod
才能做到这一点(参见 Creating function with variable number of arguments or parameters in Dart)
在class级别:
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
的代码完成并避免警告。
我主要是在寻找 JavaScript arguments
功能,但在 Dart 中。
这在 Dart 中可行吗?
你必须玩 noSuchMethod
才能做到这一点(参见 Creating function with variable number of arguments or parameters in Dart)
在class级别:
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
的代码完成并避免警告。