【发布时间】:2016-06-02 14:40:54
【问题描述】:
我想使用 List 定义的 case class match Seq[Byte],但是发生了编译器错误。
带有编译器错误的用例类
case class :: (head: Byte, tail: Seq[Byte])
def doMatchWithCaseClass(queue: Seq[Byte]) = {
queue match {
case h :: t => println("Good!") //can't compile
case _ => println("God!>_<")
}
}
doMatchWithCaseClass(Seq(1,2,3))
编译器错误:
Error:(93, 14) constructor cannot be instantiated to expected type;
found : ::
required: Seq[Byte]
case h :: t => println("Good!") //can't compile
^
使用@isaias-b 邮政编码更新
final case class :::: (override val head: Int, override val tail: Seq[Int]) extends Seq[Int] {
override def length: Int = tail.length + 1
override def iterator: Iterator[Int] = (head :: tail.toList).toIterator
override def apply(idx: Int) = {
1.toByte // just for simple
}
}
匹配码:
def doMatchWithCaseClass(queue: Seq[Int]) = {
queue match {
case h :::: t => println("case class - Good! ^_^")
case x =>
println(s"case class - God >_<! $x")
}
}
测试代码:
doMatchWithCaseClass(Seq(1,2,3))
控制台结果:
> case class - God >_<! List(1, 2, 3)
上面的代码没有任何编译错误,但这不是我的预期结果。
希望有人能指出错误。谢谢
【问题讨论】:
标签: scala pattern-matching extractor