我不明白爲什麼摺疊不能編譯。誰能給我一個線索?Scala:實現通用摺疊
sealed trait ListG[A] {
def fold[A,B](end: B, f: (A,B) => B): B = this match {
case End() => end
case Cons(hd,tl) => f(hd, tl.fold(end,f))
}
}
錯誤:(20,28)類型不匹配; found:hd.type(with underlying type A) required:A case Cons(hd,tl)=> f(hd,tl.fold(end,f)) ^ final case class EndA extends ListG [A ] 最終案例類缺點[A](HD:A,TL:ListG [A])擴展ListG [A]
將錯誤消息與代碼一起發佈 –