Scala is highly expression-oriented: most things are expressions rather than statements.
if
val color: String = if (brand == "TI") {
"blue"
} else if (brand == "HP") {
"black"
} else {
"white"
}
try
@TODO
match
基于值、类型、类、元组的模式匹配都是面向表达式的
val tup = ("localhost", 80) //> tup : (String, Int) = (localhost,80)
tup._1 //> res0: String = localhost
tup._2 //> res1: Int = 80
val result = tup match {
case ("localhost",80)=>"default"
case _ => "others"
} //> result : String = default