Created
March 30, 2018 07:29
-
-
Save notxcain/4848f0c351ce1af305b619a0ba1c382b to your computer and use it in GitHub Desktop.
Opaque Sealed Hierarchy
This file contains hidden or 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
| // Tired of subtyping and `extends Product with Serializable`? | |
| // It is impossible to instantiate a concrete subtype of `Root` | |
| // It preserves pattern matching exhaustiveness check | |
| sealed abstract class Root | |
| object Root { | |
| private final case class LeafInt(i: Int) extends Root | |
| object LeafInt { def apply(i: Int): Root = new LeafInt(i) } | |
| private final case class LeafString(s: String) extends Root | |
| object LeafString { def apply(s: String): Root = new LeafString(s) } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment