Skip to content

Instantly share code, notes, and snippets.

@Sfshaza
Last active December 13, 2016 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sfshaza/4a2a9bc2242042ba5338533d091213c0 to your computer and use it in GitHub Desktop.
Save Sfshaza/4a2a9bc2242042ba5338533d091213c0 to your computer and use it in GitHub Desktop.
Sound Dart Guide: generic type assignment
// 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