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
本文介绍了Scala语言中表达式导向的设计理念,通过示例展示了条件表达式(如if...else)、模式匹配等语法如何实现简洁且强大的功能。此外,还通过元组的使用进一步说明了基于模式匹配的灵活性。
1100

被折叠的 条评论
为什么被折叠?



