pattern&conditional
Conditional Expressions
def example(x: Int) = if x>=0 x else x
has assert built-in
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
convenientaly store and match on class contents(i.e insted of if comp.brand == "HP")
ez equality and toString
Using with match
Last updated