Skip to content

Instantly share code, notes, and snippets.

@chrislewis
Last active August 29, 2015 14:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrislewis/76bbef9c8240b55ebae1 to your computer and use it in GitHub Desktop.
Save chrislewis/76bbef9c8240b55ebae1 to your computer and use it in GitHub Desktop.
Not so sealed
sealed trait Foo
case class Bar(x: Int) extends Foo
case class Baz(name: String) extends Foo
object Foo {
def show(f: Foo) =
f match {
case Bar(x) => x.toString // Quux, a subtype of Bar in a different file, will be matched as a Bar
case Baz(name) => name
}
}
// Per http://www.scala-lang.org/files/archive/spec/2.11/05-classes-and-objects.html#sealed, we can extend
// members of the sealed hierarchy in separate units.
case class Quux(x: Int) extends Bar(x)
object Test extends App {
println(Foo.show(Quux(2)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment