匹配表达式
匹配表达式的语法优先级已更改。match 仍然是一个关键字,但它像字母运算符一样使用。这有几个后果
-
match表达式可以链接xs match { case Nil => "empty" case _ => "nonempty" } match { case "empty" => 0 case "nonempty" => 1 }(或者,去掉可选的大括号)
xs match case Nil => "empty" case _ => "nonempty" match case "empty" => 0 case "nonempty" => 1 -
match可以跟随一个句点if xs.match case Nil => false case _ => true then "nonempty" else "empty" -
匹配表达式的匹配对象必须是
InfixExpr。以前匹配对象可以后跟类型断言: T,但现在不再支持。因此,x : T match { ... }现在必须写成(x: T) match { ... }。
语法
匹配表达式的新的语法如下。
InfixExpr ::= ...
| InfixExpr MatchClause
SimpleExpr ::= ...
| SimpleExpr ‘.’ MatchClause
MatchClause ::= ‘match’ ‘{’ CaseClauses ‘}’
本文介绍