-
-
Save ochafik/7f7d603f72ef0a457e1d to your computer and use it in GitHub Desktop.
Dart Closure Placeholders
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| final _ = placeholder; | |
| main() { | |
| var list = [1, 2, 3]; | |
| print(list.map(_ + 2).map(_ * 10)); | |
| print(list.where(_ == 2).map(_.runtimeType)); | |
| print(list.map((v) => v + 2).map((v) => v * 10)); | |
| print(list.where((v) => v == 2).map((v) => v.runtimeType)); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library placeholder; | |
| import 'dart:mirrors'; | |
| class Placeholder { | |
| noSuchMethod(Invocation i) { | |
| var name = i.memberName; | |
| var pos = i.positionalArguments; | |
| withMirror(InstanceMirror f(InstanceMirror)) => | |
| (v) => f(reflect(v)).reflectee; | |
| return | |
| i.isGetter ? withMirror((m) => m.getField(name)) : | |
| i.isSetter ? withMirror((m) => m.setField(name, pos.single)) : | |
| withMirror((m) => m.invoke(name, pos, i.namedArguments)); | |
| } | |
| toString() => (v) => v.toString(); | |
| get hashCode => (v) => v.hashCode; | |
| operator==(other) => (v) => v == other; | |
| get runtimeType => (v) => v.runtimeType; | |
| } | |
| final placeholder = new Placeholder() as dynamic; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment