Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Scala
Posted by: Pahay Ragh
Added: Oct 25, 2020 4:03 PM
Modified: Oct 25, 2020 4:52 PM
Views: 8
Tags: scala
  1.   class Dog(name:String) extends Animal
  2.  
  3.   // if DOG <:Animal does List[Dog]  <: List[Animal]
  4.  
  5.   val laika = new Dog("Laika")
  6.   val lassie = new Dog("lassie")
  7.   val hachi = new Dog("hachi")
  8.  
  9.   val myDogs = List(lassie,hachi,laika)
  10.   val myAnimals: List[Animal] = List(lassie, hachi, laika)
  11.  
  12.   class CovarList[+T]
  13.   val covarDogs:CovarList[Animal] = new CovarList[Dog]
  14.  
  15.   class InvarList[T]
  16.   val invarAnimals:InvarList[Animal] = new InvarList[Animal]
  17.   val invarDogs:InvarList[Dog] = new InvarList[Dog]
  18.  
  19.   class ContraList[-T] // -T <: Animal if we wanted to impose a type constraint Scala Type Bounds
  20.   val contraDogs:ContraList[Dog] = new ContraList[Animal]
  21.  
  22.   //If your Generic Type Contains or Creates element of Type [T] it should be [+T]
  23.   //
  24.   //If your Generic Type Acts on or Consumes element of Type [T] it should be [-T]
  25.   //
  26.   //
  27.   //Covariant
  28.   //Cage , Garage , Factory , List
  29.   //
  30.   //ContraVariant
  31.   //Vet, Mechanic , Garbage pit , a Function
  32.   //Function can act on is applied on its arguments.