Created
April 4, 2020 09:29
-
-
Save polyglotpiglet/07372dfaf4cc3b8e3f2f199fcc350f9d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object Main extends App { | |
| /* | |
| Contravariant: F[B] is a subtype of F[A] if A is a subtype of B | |
| */ | |
| trait Shape {} | |
| case class Circle(radius: Double) extends Shape | |
| /* | |
| if we change this to [A] or [+A] we cannot pass | |
| covariantShape into func | |
| */ | |
| trait Contravariant[-A] { | |
| def print(): Unit | |
| } | |
| val covariantCircle: Contravariant[Circle] = () => println(s"Circle") | |
| val covariantShape: Contravariant[Shape] = () => println(s"Shape") | |
| def func(c: Contravariant[Circle]): Unit = c.print() | |
| func(covariantCircle) | |
| func(covariantShape) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment