- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Why is @tailrec rejecting this function?
Tue, 2009-06-16, 21:30
When compiling the following, I get the error "could not optimize
@tailrec annotated method", which I presume means that the code is not
tail-call compatible. I'm fairly new to functional programming, so what
am I doing wrong?
@tailrec
private def extractName(attributes:List[String],
obj:ModelObject):Map[String,String] = {
attributes match {
case head :: tail =>
// following line produces Option[String]
val value = obj.owner.getNameAttribute(obj.obj, head)
val namePair = value match {
case Some(s) => Map(head -> s)
case None => Map.empty
}
namePair ++ extractName(tail, obj)
case Nil => Map.empty
}
}
Briefly, given a List of attribute names, build up a Map of attribute
name -> value.
Thanks,
Gordon
Tue, 2009-06-16, 22:07
#2
Re: Why is @tailrec rejecting this function?
Jorge Ortiz wrote:
> This line:
>
> namePair ++ extractName(tail, obj)
>
> Is two method calls:
>
> val tmp = extractName(tail, obj)
> namePair.++(tmp)
>
> For it to be properly tail recursive, extractName needs to be the very
> last method call in the function.
Ok, I think I understand how that breaks down. But this implies that
what I'm trying to do is impossible to do in a tail recursive fashion?
Thanks,
Gordon
Tue, 2009-06-16, 22:17
#3
Re: Why is @tailrec rejecting this function?
Ok, I think I understand how that breaks down. But this implies that what I'm trying to do is impossible to do in a tail recursive fashion?
In general, when you need to accumulate the results of a recursive call, it can (usually) be transformed into a correctly tail-recursive function by moving the accumulator (the Map[String, String], in your case) to an additional parameter value (rather than the return value).
- Colin
namePair ++ extractName(tail, obj)
Is two method calls:
val tmp = extractName(tail, obj)
namePair.++(tmp)
For it to be properly tail recursive, extractName needs to be the very last method call in the function.
--j
On Tue, Jun 16, 2009 at 1:30 PM, Gordon Tyler <gordon@doxxx.net> wrote: