Scala tailrec注释错误

发布于 2024-10-09 02:54:58 字数 1054 浏览 5 评论 0原文

我有一个名为 ImmutableEntity 的 Java 抽象类和几个包含名为 @DBTable 的类级注释的子类。我正在尝试使用尾递归 Scala 方法在类层次结构中搜索注释:

  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 带注释的方法:它使用不同类型的参数递归调用”。我的内在方法有什么问题吗?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只为一人 2024-10-16 02:54:58

这是因为编译器通过循环实现尾递归的方式。这是作为从 Scala 到 Java 字节码的一系列转换中的一个步骤来完成的。每次转换都必须生成一个类型再次正确的程序。但是,如果您无法在循环执行中更改变量的类型,这就是编译器无法扩展为类型正确的循环的原因。

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.

清风夜微凉 2024-10-16 02:54:58

我可以建议一个更简洁的代码版本吗?

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)
}

}

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)
}

}

阳光的暖冬 2024-10-16 02:54:58

由于类型参数 B 及其绑定并不是严格必需的,因此您可以使用存在类型来代替,

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

Scala 接受尾递归调用的此定义。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文