Skip to content

Instantly share code, notes, and snippets.

@ochafik
Last active August 29, 2015 14:11
Show Gist options
  • Select an option

  • Save ochafik/7f7d603f72ef0a457e1d to your computer and use it in GitHub Desktop.

Select an option

Save ochafik/7f7d603f72ef0a457e1d to your computer and use it in GitHub Desktop.
Dart Closure Placeholders
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));
}
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