Created
January 23, 2020 05:06
-
-
Save lewisjkl/27b24dee4ec64b0c1b004bb154770ec3 to your computer and use it in GitHub Desktop.
ADTs in Dotty are encoded as Enums instead of a sealed hierarchy.
This file contains 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
// 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