【发布时间】:2018-05-04 16:13:46
【问题描述】:
我有以下代码,旨在在同一个提取器中定义 unapply 和 unapplySeq
test("pattern matching define unapply and unapplySeq") {
object A {
def unapply(arg: String): Option[(String, String)] = Some((arg, arg))
def unapplySeq(arg: String): Option[Seq[String]] = Some(arg.split(" "))
}
def x = "hello world"
x match {
case A(a, b) => println("unapply matched", a, b)
case A(a, b, _*) => println("unapplySeq matched", a, b)
}
}
但它似乎不起作用,当我运行这个测试用例时,它会导致编译错误并抱怨
Error:(292, 12) Star pattern must correspond with varargs or unapplySeq
case A(a, b, _*) => println("unapplySeq matched", a, b)
我想知道是否可以在同一个提取器中同时定义 unapply 和 unapplySeq?
【问题讨论】:
标签: scala