pattern&conditional
Conditional Expressions
Pattern Matching
val times = 1
times match {
case 1 => "one"
case 2 => "two"
case _ => "some other number"
}
//Matching with guards
times match {
case i if i == 1 => "one"
case i if i == 2 => "two"
case _ => "some other number"
}
//Matching on Type
def bigger(o: Any): Any = {
o match {
case i: Int if i < 0 => i - 1
case i: Int => i + 1
case d: Double if d < 0.0 => d - 0.1
case d: Double => d + 0.1
case text: String => text + "s"
}
}Case Classes
Using with match
Last updated