Skip to content

Commit 6b2a16a

Browse files
committed
Fix the example for contravariance
In the example `printer: Printer[Cat]` parameter is substituted for a `Printer[Animal`, not the other way around
1 parent 71107cf commit 6b2a16a

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

_tour/variances.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class CatPrinter extends Printer[Cat] {
7878
}
7979
```
8080

81-
If a `Printer[Cat]` knows how to print any `Cat` to the console, and a `Printer[Animal]` knows how to print any `Animal` to the console, it makes sense that a `Printer[Animal]` would also know how to print any `Cat`. The inverse relationship does not apply, because a `Printer[Cat]` does not know how to print any `Animal` to the console. Therefore, we should be able to substitute a `Printer[Animal]` for a `Printer[Cat]`, if we wish, and making `Printer[A]` contravariant allows us to do exactly that.
81+
If a `Printer[Cat]` knows how to print any `Cat` to the console, and a `Printer[Animal]` knows how to print any `Animal` to the console, it makes sense that a `Printer[Animal]` would also know how to print any `Cat`. The inverse relationship does not apply, because a `Printer[Cat]` does not know how to print any `Animal` to the console. Therefore, we should be able to substitute a `Printer[Cat]` for a `Printer[Animal]`, if we wish, and making `Printer[A]` contravariant allows us to do exactly that.
8282

8383
```tut
8484
def printMyCat(printer: Printer[Cat], cat: Cat): Unit =

0 commit comments

Comments
 (0)