Skip to content

Instantly share code, notes, and snippets.

@lewisjkl
Created January 23, 2020 05:06
Show Gist options
  • Save lewisjkl/27b24dee4ec64b0c1b004bb154770ec3 to your computer and use it in GitHub Desktop.
Save lewisjkl/27b24dee4ec64b0c1b004bb154770ec3 to your computer and use it in GitHub Desktop.
ADTs in Dotty are encoded as Enums instead of a sealed hierarchy.
// Note - these examples are not meant to show how the `Option` type is actually implemented
// Scala 2.13.x
sealed abstract class Option[+A]
case class Some[A](x: A) extends Option[A]
case object None extends Option[Nothing]
Some("test")
// has type Some[String]
// Dotty
enum Option[+A] {
case Some(x: A) // implied `extends Option[A]` here
case None // implied `extends Option[Nothing]` here since A is covariant
}
// Note: if A were contravariant, then `extends Option[Any]` would be implied for the `None` case
Option.Some("test")
// has type Option[String] because the apply method generated for `Some[A]` returns `Option[A]`
new Option.Some("test")
// has type Option.Some[String] because apply method wasn't used
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment