【发布时间】:2014-09-29 17:43:09
【问题描述】:
我正在尝试匹配列表的前两个元素,但是,它不会接受任意长度的列表。以下代码失败。
def demoCases() = {
def actor1 = new Actor[Employee] {}
def actor2 = new Actor[Employee] {}
def actor3 = new Actor[Animal] {}
var actors = List(actor1, actor2, actor3);
println();
actors match {
case (_: Employee) :: (_: Employee) :: tail
=> {println("nice 2 employees to start with ")};
case Nil
=> {println("no match")}
}
例外:
Exception in thread "main" scala.MatchError: List(.....)
如何指定只有列表的两个元素first需要匹配这个表达式?
【问题讨论】:
-
=>后面不需要 { }
标签: list scala generics pattern-matching match