Skip to content

Instantly share code, notes, and snippets.

@polyglotpiglet
Created April 4, 2020 09:29
Show Gist options
  • Select an option

  • Save polyglotpiglet/07372dfaf4cc3b8e3f2f199fcc350f9d to your computer and use it in GitHub Desktop.

Select an option

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