如何在 Scala 2.8 注释中指定静态数组? [英] How do I specify a static array in a Scala 2.8 annotation?

查看:24
本文介绍了如何在 Scala 2.8 注释中指定静态数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 Hibernate Annotations 3.4.0 在 Scala 2.8.0 中构建一些带注释的域类.它一直工作正常,只是有某些注释将数组作为参数.例如,这里有一个我想在 Scala 中表达的 Java 注释:

I've been building out some annotated domain classes in Scala 2.8.0 using Hibernate Annotations 3.4.0. It's been working fine, except that there are certain annotations which take an array as a parameter. For example, here's a Java annotation that I want to express in Scala:

@OneToMany(mappedBy="passport_id", cascade=CascadeType.PERSIST)

然而,注解需要一个数组/集合作为输入:

However, the annotation requires an array/set as input:

[ERROR] .../Passport.scala:50: error: type mismatch; 
[INFO]  found   : javax.persistence.CascadeType(value PERSIST)
[INFO]  required: Array[javax.persistence.CascadeType]
[INFO]     @OneToMany(mappedBy="passport_id", cascade=CascadeType.PERSIST)

我尝试了各种括号、方括号/角括号/大括号等:

I've tried various parentheses, square/angle/curly brackets, and so on:

@OneToMany(mappedBy="passport_id", cascade=(CascadeType.PERSIST))
@OneToMany(mappedBy="passport_id", cascade=[CascadeType.PERSIST])
@OneToMany(mappedBy="passport_id", cascade=<CascadeType.PERSIST>)
@OneToMany(mappedBy="passport_id", cascade={CascadeType.PERSIST})

...但不幸的是,我对 Scala/Java 注释的理解已经到了尽头.感谢帮助.

... but unfortunately I've reached the end of my understanding of Scala/Java annotations. Help is appreciated.

推荐答案

我将添加一些来自 spec 解释为什么 Rex 的解决方案有效.

I'll add a few snippets from the spec to explain why Rex's solution works.

对于 JVM 上的 Scala,将保留在生成的类中的注释的参数必须是常量表达式:

For Scala on the JVM, arguments to annotations that will be retained within the generated class must be constant expressions:

从 trait 继承的注解类的实例scala.ClassfileAnnotation 将存储在生成的类文件中....此外,在 Java 和 .NET 上,所有构造函数参数都必须是常量表达式.

Instances of an annotation class inheriting from trait scala.ClassfileAnnotation will be stored in the generated class files. ... Additionally, on both Java and .NET, all constructor arguments must be constant expressions.

什么是常量表达式?

6.24 常量表达式 常量表达式是Scala 编译器可以评估为持续的.常数"的定义表达"取决于平台,但它们至少包括以下形式的表达式:

6.24 Constant Expressions Constant expressions are expressions that the Scala compiler can evaluate to a constant. The definition of "constant expression" depends on the platform, but they include at least the expressions of the following forms:

  • 值类的文字,例如整数
  • 字符串文字
  • 使用 Predef.classOf (§12.4) 构造的类
  • 来自底层平台的枚举元素
  • 一个文字数组,形式为 Array(c1, . . . , cn),其中所有的 ci 本身是常数表达
  • 由常量值定义(第 4.1 节)定义的标识符.

您还应该能够将参数重构为 final val.然而,这似乎不适用于数组.我会提出一个错误.

You should also be able to refactor the argument to a final val. This doesn't seem to work for Arrays, however. I'll raise a bug.

class T(value: Any) extends ClassfileAnnotation

object Holder {
   final val as = Array(1, 2, 3)
   final val a = 1
} 

@T(Holder.a)
@T(Holder.as)  // annot.scala:9: error: annotation argument needs to be a constant; found: Holder.as
class Target

这篇关于如何在 Scala 2.8 注释中指定静态数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆