我有一個名爲ImmutableEntity
的Java抽象類和幾個包含稱爲@DBTable
的類級註釋的子類。我試圖用尾遞歸方法斯卡拉搜索的註釋的類層次結構:Scala tailrec註釋錯誤
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)
}
}
}
當我編譯這段代碼,我得到了錯誤:「無法優化@tailrec註釋的方法:它被稱爲遞歸地用不同的類型參數「。我內心的方法有什麼問題?
謝謝。
感謝您的解釋。順便說一句,偉大的語言! – Ralph 2010-12-24 13:03:11