Last active
December 20, 2019 04:14
-
-
Save andhieka/6b6b64a79924a604000686cbf821ea90 to your computer and use it in GitHub Desktop.
Proof that Dart enum extension works
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
enum FruitType { | |
orange, | |
apple, | |
guava, | |
} | |
extension FruitTypeDescriptions on FruitType { | |
String get name { | |
switch (this) { | |
case FruitType.orange: | |
return "Orange"; | |
case FruitType.apple: | |
return "Apple"; | |
case FruitType.guava: | |
return "Guava"; | |
} | |
} | |
String unitNameForAmount(int amount) { | |
if (amount == 1) { | |
return name; | |
} else { | |
return name + "s"; | |
} | |
} | |
String isSimilarTo(FruitType otherFruit) { | |
if (this == otherFruit) { | |
return "Yes"; | |
} else { | |
return "No"; | |
} | |
} | |
} | |
void main() { | |
for (FruitType fruitType in FruitType.values) { | |
print("## $fruitType"); | |
print("Name: ${fruitType.name}"); | |
print("Plural Form: ${fruitType.unitNameForAmount(2)}"); | |
print("Similar to guava: ${fruitType.isSimilarTo(FruitType.guava)}"); | |
print(""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment