Basic examples of using call() and Function.apply() in Dart
Find this at dartpad.dartlang.org/df1fec65cf0b91a1dfa2f94dc4e43680.
Basic examples of using call() and Function.apply() in Dart
Find this at dartpad.dartlang.org/df1fec65cf0b91a1dfa2f94dc4e43680.
class Adder implements Function { | |
call(int a, int b) => a + b; | |
} | |
class Incrementer implements Function { | |
int _amt; | |
Incrementer(this._amt); | |
call(int a) => a + _amt; | |
} | |
Function f = (int n, int m, {operation: "add"}) { | |
if (operation == "add") { | |
return n + m; | |
} else { | |
return n - m; | |
} | |
}; | |
main() { | |
Adder myAdder = new Adder(); | |
Incrementer myIncrementer = new Incrementer(2); | |
print(myAdder(10, 3)); | |
print(myIncrementer(40)); | |
int a = Function.apply(f, [10,3]); | |
print(a); | |
int b = Function.apply(f, [10,3], {new Symbol("operation"): "subtract"}); | |
print(b); | |
} |
Too bad, there is no way of doing the "apply" thingy with a class
Good example