Skip to content

Instantly share code, notes, and snippets.

@andhieka
Last active December 20, 2019 04:14
Show Gist options
  • Save andhieka/6b6b64a79924a604000686cbf821ea90 to your computer and use it in GitHub Desktop.
Save andhieka/6b6b64a79924a604000686cbf821ea90 to your computer and use it in GitHub Desktop.
Proof that Dart enum extension works
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