Skip to content

Instantly share code, notes, and snippets.

@MelbourneDeveloper
Created May 11, 2023 21:44
Show Gist options
  • Save MelbourneDeveloper/68bfd14dd9f66473f382c5b732fae405 to your computer and use it in GitHub Desktop.
Save MelbourneDeveloper/68bfd14dd9f66473f382c5b732fae405 to your computer and use it in GitHub Desktop.
Pattern Matching Example
abstract class Animal {
String get name;
}
class Dog extends Animal {
@override
String get name => 'Spot';
}
class Cat extends Animal {
@override
String get name => 'Garfield';
}
void main() {
Animal pet = Dog();
final sound = switch (pet) {
(Dog d) => '${d.name}: Woof!',
(Cat c) => '${c.name}: Meow!',
_ => '...',
};
print(sound);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment