Last active
August 29, 2015 14:05
-
-
Save longliveenduro/9e4d644ab1404eb188b2 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
case class User(id: Int, name: String, avatarUrl: Option[String]) | |
val kevin = User(1, "Kevin", None) | |
val jeremy = User(1, "Jeremy", Some("http://someurl.de")) | |
val users = Seq(kevin, jeremy) | |
users.forEach { // iterate over the user sequence | |
user => user match { // match every entry in the value 'user' | |
case User(name, Some(url)) => | |
println (s"User $name has an avatar at $url") // this executes only if it is a User with an URL set | |
case User(name, None) => | |
println (s"User $name has no avatar") // this executes only if it is a User with no URL set | |
case other => | |
println(s"not a user but a $other") // this executes in all other cases and binds the object to the name 'other' | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment