Skip to content

Instantly share code, notes, and snippets.

@kranfix
Last active March 29, 2020 22:02
Show Gist options
  • Save kranfix/82ccca5089150eadbf24a258d9d5be02 to your computer and use it in GitHub Desktop.
Save kranfix/82ccca5089150eadbf24a258d9d5be02 to your computer and use it in GitHub Desktop.
// Python example: https://gist.github.com/kranfix/f3b8cf554b25b6c6f7e9524aec6bf0af
// JavaScript example: https://gist.github.com/kranfix/4b1774e940cc112098e7e559fb5a8323
// Dart example: https://gist.github.com/kranfix/82ccca5089150eadbf24a258d9d5be02
// Dartpad: https://dartpad.dev/82ccca5089150eadbf24a258d9d5be02
void main() {
final foo = Foo(name: "Simple", counter: 20);
print(foo);
foo.counter = 21;
foo.name = "Not simple";
print(foo);
final rflx = ReflexFoo(name: "Reflex", counter: 30);
print(rflx);
rflx.counter++;
print(rflx["counter"]);
print(rflx);
}
class Foo {
Foo({this.name, this.counter});
int counter;
String name;
@override
toString() => '[$runtimeType:$name] counter=$counter';
}
class ReflexFoo {
ReflexFoo({String name, int counter}) : _map = {
"name": name,
"counter": counter,
};
Map<String, dynamic> _map;
int get counter => _map["counter"];
set counter(int value) => _map["counter"] = value;
String get name => _map["name"];
set name(String value) => _map["counter"] = value;
dynamic operator [](String key) => _map[key];
void operator []=(String key, dynamic value) => _map[key] = value;
@override
toString() => '[$runtimeType:$name] counter=$counter';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment