Last active
December 13, 2016 18:11
-
-
Save Sfshaza/4a2a9bc2242042ba5338533d091213c0 to your computer and use it in GitHub Desktop.
Sound Dart Guide: generic type assignment
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
// Enable strong mode by clicking Strong mode checkbox in lower right. | |
// The code passes strong mode static analysis. | |
// Follow the instructions in the generic type assignment below. | |
class Animal { | |
void feed() {} | |
} | |
class Alligator extends Animal {} | |
class Cat extends Animal {} | |
class MaineCoon extends Cat {} | |
void feedAnimals(Iterable<Animal> animals) { | |
for (var animal in animals) { | |
animal.feed(); | |
} | |
} | |
main(List<String> args) { | |
List<Cat> myCats = new List<Cat>(); // Replace Cat with MaineCoon. It passes. | |
// Replace with Animal. It passes. | |
// Replace with Alligator. It fails. | |
Cat muffin = new Cat(); | |
Cat winky = new Cat(); | |
Cat bongo = new Cat(); | |
myCats.addAll([muffin, winky, bongo]); | |
feedAnimals(myCats); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment