Snippets

Oleg Chulakov Studio Ковариантность и контравариантность

Updated by Chulakov Deploy

File types.ts Modified

  • Ignore whitespace
  • Hide word diff
     say(): string {
         return super.say() + " and I am is a cat";
     }
+    meow(): string {
+        return "Meow";
+    }
 }
 
 class Dog extends Animal {
     say(): string {
         return super.say() + " and I am is a dog";
     }
+    woof(): string {
+        return "Woof";
+    }
 }
Updated by Chulakov Deploy

File contravariance.ts Added

  • Ignore whitespace
  • Hide word diff
+class JuniorCatDoctor
+{
+    heal(cat: Cat): void {
+        // Many code to heal cat
+    }
+}
+
+class SeniorCatDoctor extends JuniorCatDoctor
+{
+    heal(cat: Animal): void {
+        // If cat use standart code to heal cat
+        // Else analises animal and try to heal
+    }
+}
+
+const cat = new Cat('Murka');
+const dog = new Dog('Sharik');
+
+const juniorDoctor = new JuniorCatDoctor();
+const seniorDoctor = new SeniorCatDoctor();
+
+juniorDoctor.heal(cat);
+juniorDoctor.heal(dog); // Kernel panic!
+
+seniorDoctor.heal(cat);
+seniorDoctor.heal(dog); // Dog still live

File covariance.ts Added

  • Ignore whitespace
  • Hide word diff
+interface AnimalRepository
+{
+    fetchAnimal(): Animal;
+}
+
+class CatRepository implements AnimalRepository
+{
+    fetchAnimal(): Cat {
+        return new Cat('Murka');
+    }
+}
+
+class DogRepository implements AnimalRepository
+{
+    fetchAnimal(): Dog {
+        return new Dog('Sharik');
+    }
+}
+
+const cat = (new CatRepository()).fetchAnimal();
+const dog = (new DogRepository()).fetchAnimal();
+
+console.log(cat.say()); // My name is Murka and I am is a cat
+console.log(dog.say()); // My name is Sharik and I am is a dog

File types.ts Added

  • Ignore whitespace
  • Hide word diff
+class Animal {
+  name: string;
+  constructor(name: string) {
+    this.name = name;
+  }
+  say(): string {
+    return "My name is " + this.name;
+  }
+}
+
+class Cat extends Animal {
+    say(): string {
+        return super.say() + " and I am is a cat";
+    }
+}
+
+class Dog extends Animal {
+    say(): string {
+        return super.say() + " and I am is a dog";
+    }
+}
Created by Chulakov Deploy

File contravariance.php Added

  • Ignore whitespace
  • Hide word diff
+<?php
+class JuniorCatDoctor
+{
+    public function heal(Cat $cat): void
+    {
+        // Many code to heal cat
+    }
+}
+
+class SeniorCatDoctor extends JuniorCatDoctor
+{
+    public function heal(Animal $cat): void
+    {
+        // If cat use standart code to heal cat
+        // Else analises animal and try to heal
+    }
+}
+
+$cat = new Cat('Murka');
+$dog = new Dog('Sharik');
+
+$juniorDoctor = new JuniorCatDoctor();
+$seniorDoctor = new SeniorCatDoctor();
+
+$juniorDoctor->heal($cat);
+// $juniorDoctor->heal($dog); // Kernel panic!
+
+$seniorDoctor->heal($cat);
+$seniorDoctor->heal($dog); // Dog still live

File covariance.php Added

  • Ignore whitespace
  • Hide word diff
+<?php
+interface AnimalRepository
+{
+    public function fetchAnimal(): Animal;
+}
+
+class CatRepository implements AnimalRepository
+{
+    public function fetchAnimal(): Cat
+    {
+        return new Cat('Murka');
+    }
+}
+
+class DogRepository implements AnimalRepository
+{
+    public function fetchAnimal(): Dog
+    {
+        return new Dog('Sharik');
+    }
+}
+
+$cat = (new CatRepository())->fetchAnimal();
+$dog = (new DogRepository())->fetchAnimal();
+
+echo $cat->say(); // My name is Murka and I am is a cat
+echo "\n";
+echo $dog->say(); // My name is Sharik and I am is a dog

File types.php Added

  • Ignore whitespace
  • Hide word diff
+<?php
+class Animal
+{
+    public function __construct(
+        private string $name,
+    ) {}
+    
+    public function say(): string
+    {
+        return "My name is {$this->name}";
+    }
+}
+
+class Cat extends Animal
+{
+    public function say(): string
+    {
+        return parent::say() . " and I am is a cat";
+    }
+}
+
+class Dog extends Animal
+{
+    public function say(): string
+    {
+        return parent::say() . " and I am is a dog";
+    }
+}
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.