Created
February 9, 2015 13:51
-
-
Save shigemk2/192a5f4e670077ac6e3b to your computer and use it in GitHub Desktop.
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
trait Animal | |
case class Dog(name: String) extends Animal | |
case class Cat(name: String) extends Animal | |
case object Woodpecker extends Animal | |
case class Lion(name: String) extends Animal | |
object CaseClassTest extends App { | |
def determineType(x: Animal): String = x match { | |
case Dog(moniker) => "Got a Dog, name = " + moniker | |
case _:Cat => "Got a Cat (ignoring the name)" | |
case Woodpecker => "That was a Woodpecker" | |
case _ => "That was something else" | |
} | |
println(determineType(new Dog("Rocky"))) | |
println(determineType(new Cat("Rusty the Cat"))) | |
println(determineType(Woodpecker)) | |
println(determineType(new Lion("LION"))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment