Skip to content

Instantly share code, notes, and snippets.

@4lex1v
Created September 18, 2014 17:32
Show Gist options
  • Save 4lex1v/3d8e9d56108c2394a2c7 to your computer and use it in GitHub Desktop.
Save 4lex1v/3d8e9d56108c2394a2c7 to your computer and use it in GitHub Desktop.
Variance, bottom types and type inference in Scala
/*****************************
* Invariant type with Nothing *
****************************/
sealed trait Interact[A]
case class Ask(value: String) extends Interact[String]
case class Tell(value: String) extends Interact[Nothing] // Notice Nothing here
val instr =
List(Ask("What's your first name?"),
Ask("What's your last name?"),
Tell("Hello, ???"))
instr: List[Product with Serializable with Interact[_ <: String]] = ???
------------------------------------------------------------------------------------
/*****************************
* Invariant type with Unit *
****************************/
sealed trait Interact[A]
case class Ask(value: String) extends Interact[String]
case class Tell(value: String) extends Interact[Unit] // And Unit here
val instr =
List(Ask("What's your first name?"),
Ask("What's your last name?"),
Tell("Hello, ???"))
instr: List[Product with Serializable with Interact[_ >: Unit with String]] = ???
------------------------------------------------------------------------------------
/********************************
* Covariant type with Nothing *
*******************************/
sealed trait Interact[+A]
case class Ask(value: String) extends Interact[String]
case class Tell(value: String) extends Interact[Nothing]
val instr =
List(Ask("What's your first name?"),
Ask("What's your last name?"),
Tell("Hello, ???"))
instr: List[Product with Serializable with Interact[String]] = ???
------------------------------------------------------------------------------------
/*****************************
* Covariant type with Unit *
****************************/
sealed trait Interact[+A]
case class Ask(value: String) extends Interact[String]
case class Tell(value: String) extends Interact[Unit]
val instr =
List(Ask("What's your first name?"),
Ask("What's your last name?"),
Tell("Hello, ???"))
instr: List[Product with Serializable with Interact[Any]] = ???
------------------------------------------------------------------------------------
/************************************
* Contravariant type with Nothing *
***********************************/
sealed trait Interact[-A]
case class Ask(value: String) extends Interact[String]
case class Tell(value: String) extends Interact[Nothing]
val instr =
List(Ask("What's your first name?"),
Ask("What's your last name?"),
Tell("Hello, ???"))
instr: List[Product with Serializable with Interact[Nothing]] = ???
------------------------------------------------------------------------------------
/************************************
* Contravariant type with Unit *
***********************************/
sealed trait Interact[-A]
case class Ask(value: String) extends Interact[String]
case class Tell(value: String) extends Interact[Unit]
val instr =
List(Ask("What's your first name?"),
Ask("What's your last name?"),
Tell("Hello, ???"))
instr: List[Product with Serializable with Interact[Unit with String]] = ???
// Example was taken from Runar's talk- http://www.parleys.com/play/53a7d2c3e4b0543940d9e538
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment