Last active
August 29, 2015 14:02
-
-
Save chrislewis/76bbef9c8240b55ebae1 to your computer and use it in GitHub Desktop.
Not so sealed
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
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 | |
} | |
} |
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
// 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