Skip to content

Instantly share code, notes, and snippets.

@polyglotpiglet
Last active April 4, 2020 09:28
Show Gist options
  • Select an option

  • Save polyglotpiglet/81eff6620f02db3fe721f80eeb43894a to your computer and use it in GitHub Desktop.

Select an option

Save polyglotpiglet/81eff6620f02db3fe721f80eeb43894a to your computer and use it in GitHub Desktop.
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