Last active
April 4, 2020 09:28
-
-
Save polyglotpiglet/81eff6620f02db3fe721f80eeb43894a 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 { | |
| /* | |
| Covariant: F[B] is a subtype of F[A] if B is a subtype of A | |
| */ | |
| trait Shape {} | |
| case class Circle(radius: Double) extends Shape | |
| /* | |
| if we change this to [A] or [-A] we cannot pass | |
| covariantCircle into func | |
| */ | |
| trait Covariant[+A] { | |
| def print(): Unit | |
| } | |
| val covariantCircle: Covariant[Circle] = () => println(s"Circle") | |
| val covariantShape: Covariant[Shape] = () => println(s"Shape") | |
| def func(c: Covariant[Shape]): 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