我有一个我认为是相当简单的尾递归函数。但是,@tailrec
告诉我不然。
@tailrec
def _next(continue : String, current : List[Long], first : Boolean ) : Stream[Long] = {
current match {
case head :: tail => head #:: _next(continue, tail, false) //this line breaks tailrec
case Nil if first => empty
case _ => {
val (nc, nl) = getIds(continue)
_next(nc, nl, true)
}
}
}
这向我展示了
[error] could not optimize @tailrec annotated method _next: it contains a recursive call not in tail position
[error] case head :: tail => head #:: _next(continue, tail, false)
[error] ^
它可能与我从 eclipse: 收到的隐式通知有关- Implicit conversions found: _next(continue, tail, false) => consWrapper(_next(continue, tail, false))
,但不幸的是,这并不能帮助我解决问题。
我该如何解决这个问题,并且,对于布朗尼点,我认为这会尾递归在哪里出错了?