Skip to content

Instantly share code, notes, and snippets.

@tenhobi
Last active May 25, 2020 08:09
Show Gist options
  • Save tenhobi/33013a208b3909b1f3e34ae4e960dc24 to your computer and use it in GitHub Desktop.
Save tenhobi/33013a208b3909b1f3e34ae4e960dc24 to your computer and use it in GitHub Desktop.
class Animal {}
class Cat extends Animal {}
class Dog extends Animal {}
class Pencil {}
class Tool<A, B> {}
main() {
List<Cat> catList = [Cat(), Cat(), Cat()];
List<Dog> dogList = [Dog(), Dog(), Dog()];
List<Animal> animalList = [...catList, ...dogList];
for (final x in animalList) {
print(x);
}
print(catList is List<Animal>); // true
print(catList is List<Cat>); // true
print(catList is List<Dog>); // false
print(animalList is List<Cat>); // false
print(catList is List<Pencil>); // false
/// -----
/// VÍCE GENERIKY, SLOŽITĚJŠÍ PŘÍKLAD
print('-----');
var a = Tool<Animal, Animal>();
var b = Tool<Cat, Cat>();
var c = Tool<Cat, Dog>();
var d = Tool<Dog, Cat>();
print(a is Tool<Animal, Animal>); // true
print(b is Tool<Animal, Animal>); // true
print(c is Tool<Animal, Animal>); // true
print(d is Tool<Animal, Animal>); // true
print(c is Tool<Dog, Cat>); // false
print(c is Tool<Cat, Dog>); // true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment