Skip to content

Instantly share code, notes, and snippets.

@longliveenduro
Last active August 29, 2015 14:05
Show Gist options
  • Save longliveenduro/9e4d644ab1404eb188b2 to your computer and use it in GitHub Desktop.
Save longliveenduro/9e4d644ab1404eb188b2 to your computer and use it in GitHub Desktop.
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