Differences

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

Link to this comparison view

Next revision
Previous revision
pp:2024:scala:l05 [2024/03/28 21:14]
tudor.condrea created
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
     }     }
 } }
Line 98: Line 98:
  
 **5.2.3.** Implement an extension that allows you to use ''&&'',​ ''​||''​ and ''​~''​ (Unary not) in order to create expressions more easily. **5.2.3.** Implement an extension that allows you to use ''&&'',​ ''​||''​ and ''​~''​ (Unary not) in order to create expressions more easily.
 +<code scala>
 extension (e: BoolExpr) { extension (e: BoolExpr) {
     ???     ???