Last active
June 15, 2023 07:26
-
-
Save PlugFox/ed18b5ce21c5286b5a95d2efa34958f1 to your computer and use it in GitHub Desktop.
Enum with Expando
This file contains 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
/* | |
* Enum with expando properties | |
* https://gist.github.com/PlugFox/ed18b5ce21c5286b5a95d2efa34958f1 | |
* https://dartpad.dev/ed18b5ce21c5286b5a95d2efa34958f1 | |
* Matiunin Mikhail <plugfox@gmail.com>, 07 June 2022 | |
*/ | |
void main() { | |
const red = Color.red, green = Color.green, blue = Color.blue; | |
print('${red.color}, ${green.color}, ${blue.color}'); | |
// <unknown>, <unknown>, <unknown> | |
red.color = 'red'; | |
blue.color = 'blue'; | |
print('${red.color}, ${green.color}, ${blue.color}'); | |
// red, <unknown>, blue | |
print(Color.red.color); | |
// red | |
} | |
enum Color { | |
red, | |
green, | |
blue; | |
String get color => _expando[this] ?? '<unknown>'; | |
set color(String color) => _expando[this] = color; | |
static final Expando<String> _expando = Expando<String>(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment