Skip to content

Instantly share code, notes, and snippets.

@ilikerobots
Last active June 21, 2022 10:45
Show Gist options
  • Save ilikerobots/df1fec65cf0b91a1dfa2f94dc4e43680 to your computer and use it in GitHub Desktop.
Save ilikerobots/df1fec65cf0b91a1dfa2f94dc4e43680 to your computer and use it in GitHub Desktop.
Basic examples: Dart call() and Function.apply()
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);
}
@hydev777
Copy link

hydev777 commented Mar 3, 2022

Good example

@JokeUrSelf
Copy link

Too bad, there is no way of doing the "apply" thingy with a class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment