Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
pp:2024:scala:l05 [2024/03/29 08:41]
tudor.condrea
pp:2024:scala:l05 [2024/03/31 16:45] (current)
tudor.condrea
Line 15: Line 15:
 **5.1.1.** We introduce a carrier class. Modify the type signature so that it allows for animal subtypes. **5.1.1.** We introduce a carrier class. Modify the type signature so that it allows for animal subtypes.
 <code scala> <code scala>
-class Carrier[T](val content: T)+class Carrier[T ​<: Animal](val content: T)
 </​code>​ </​code>​
  
Line 27: Line 27:
 ==== Contravariance ==== ==== Contravariance ====
  
-We introduce a veterinarian class. Consider why this class is contravariant in ''​T''​.+We introduce a veterinarian class. Consider why this class should be contravariant in ''​T''​.
 <code scala> <code scala>
-class Vet[-T <: Animal] {+class Vet[T <: Animal] {
     def treat(patient:​ T): String = {     def treat(patient:​ T): String = {
-        ​s"Can treat $patient"+        "Can treat " ​+ patient.toString
     }     }
 } }