Как реализовать сопоставление с образцом, например, head::tail для списка, моим собственным методом?

I want to add a '+:' method to my code to realise the pattern match like that List offered

case head::tail => _

Итак, я пытаюсь написать эти коды

abstract class Num{
     def +: (b:Int):Num
}
case class Entity(a:Int, t:Num) extends Num{
    def +: (b:Int):Num = new Entity(b,this)
    override def toString:String = "Entity(" + a + "," + t + ")"
}
case object ZERO extends Num{
    def +: (b:Int):Num = new Entity(b,this)
    override def toString:String = "ZERO"
}


def toIntList(n:Num):List[Int] = n match{
    case ZERO => Nil
    case (head:Int) +: tail => head :: toIntList(tail)
}

Но плохо работают....

scala> val two = 2 +: ( 1 +: ZERO)
two: Num = Entity(2,Entity(1,ZERO))

scala> toIntList(two)
scala.MatchError: Entity(2,Entity(1,ZERO)) (of class Entity)
    at .toIntList(<console>:11)
    at .<init>(<console>:14)
    at .<clinit>(<console>)
    at .<init>(<console>:7)
    at .<clinit>(<console>)
    at $print(<console>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

person Sughiy    schedule 03.03.2014    source источник
comment
Было бы неплохо, если бы код компилировался. Что такое NUM, HEHE и hehe? Спасибо   -  person Rado Buransky    schedule 03.03.2014
comment
Дублирует stackoverflow.com/q/19426548/1296806, объясняя, что вам нужен экстрактор. Есть и другие уточняющие вопросы.   -  person som-snytt    schedule 03.03.2014
comment
@som-snytt спасибо! экстрактор потрясающий   -  person Sughiy    schedule 03.03.2014