4

I have a Java abstract class called ImmutableEntity and several subclasses that contain a class-level annotation called @DBTable. I am trying to search a class hierarchy for the annotation using a tail-recursive Scala method:

  def getDbTableForClass[A <: ImmutableEntity](cls: Class[A]): String = {
    @tailrec
    def getDbTableAnnotation[B >: A](cls: Class[B]): DBTable = {
      if (cls == null) {
        null
      } else {
        val dbTable = cls.getAnnotation(classOf[DBTable])
        if (dbTable != null) {
          dbTable
        } else {
          getDbTableAnnotation(cls.getSuperclass)
        }
      }
    }

    val dbTable = getDbTableAnnotation(cls)
    if (dbTable == null) {
      throw new
              IllegalArgumentException("No DBTable annotation on class " + cls.getName)
    } else {
      val value = dbTable.value
      if (value != null) {
        value
      } else {
        throw new
                IllegalArgumentException("No DBTable.value annotation on class " + cls.getName)
      }
    }
  }

When I compile this code, I am getting the error: "could not optimize @tailrec annotated method: it is called recursively with different type arguments". What is wrong with my inner method?

Thanks.

3 Answers 3

16

It's because of the way the compiler implements tail-recursion by loops. This is done as one step in a chain of transformations from Scala to Java bytecodes. Each transformation must produce a program that's again type-correct. However, it you can't change the type of variables in mid-loop execution, that's why the compiler could not expand into a type-correct loop.

2
  • 1
    Thanks for the explanation. BTW, great language!
    – Ralph
    Dec 24, 2010 at 13:03
  • 1
    I will also observe: what a fine, precise error message! "Could not optimize @tailrec annotated method: it is called recursively with different type arguments". Whoever is writing gems like that must be one cool cat.
    – psp
    Jan 10, 2011 at 9:17
3

May I suggest a more succinct version of the code?

def getDbTableForClass[A <: ImmutableEntity](cls: Class[A]): String = {
@tailrec
def getDbTableAnnotation[B >: A](cls: Class[B]): DBTable = cls match {
  case null => null
  case c if c.isAnnotationPresent(classOf[DBTable]) => c.getAnnotation(classOf[DBTable])
  case other => getDbTableAnnotation(other.getSuperclass)
}

getDbTableAnnotation(cls) match {
  case null => throw new IllegalArgumentException("No DBTable annotation on class " + cls.getName)
  case dbTable if dbTable.value ne null => dbTable.value
  case other => throw new IllegalArgumentException("No DBTable.value annotation on class " + cls.getName)
}

}

0
2

Since the type parameter B and its bound aren't strictly required, you can use an existential type instead,

@tailrec
def getDbTableAnnotation(cls: Class[_]): DBTable = {
  ...
}

Scala accepts this definition for tail-recursive calls.

2
  • Thanks. I'll try that. Do you know why the first form was rejected?
    – Ralph
    Dec 23, 2010 at 18:14
  • @Ralph: No, I don't know for sure. The @tailrec optimization converts the recursive function into a loop and with erasure I don't see how that could cause trouble. It could be a compiler implementation limitation or it could be that the spec doesn't allow it because the target platform may erase types like the JVM. Dec 23, 2010 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.