Skip to content

Instantly share code, notes, and snippets.

@notxcain
Created March 30, 2018 07:29
Show Gist options
  • Select an option

  • Save notxcain/4848f0c351ce1af305b619a0ba1c382b to your computer and use it in GitHub Desktop.

Select an option

Save notxcain/4848f0c351ce1af305b619a0ba1c382b to your computer and use it in GitHub Desktop.
Opaque Sealed Hierarchy
// 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