Вы можете попробовать определить экстракторы:
object CNil {
def unapply[T](clist: CList[T]): Boolean = clist.isInstanceOf[CNil[_]]
}
object CCons {
def unapply[T](clist: CList[T]): Option[(T, CList[T])] = clist match {
case _: CNil[_] => None
case c: CCons[_] => Some(c.head, c.tail)
}
}
@annotation.tailrec def lastValue[T](l: CList[T]): Option[T] = l match {
case CNil() => None
case CCons(head, tail) => lastValue(tail)
}
Возможно, вам придется дать им другое имя, если вы не можете поместить их в те же файлы, что и исходные черты.
С другой стороны, реализация вашей lastValue
функции, вероятно, не делает то, что вы ожидаете ... Как насчет этой?
def lastValue[T](clist: CList[T]): Option[T] = {
@annotation.tailrec
def lastValue0(prevValue: Option[T], clist: CList[T]): Option[T] =
clist match {
case CNil() => prevValue
case CCons(head, tail) => lastValue0(Some(head), tail)
}
lastValue0(None, clist)
}