I have constants that are made of other, smaller constants. for instance
object MyConstants { final val TABLENAME = "table_name"; final val FIELDNAME = "field_name"; final val INDEXNAME = TABLENAME + "_" + FIELDNAME + "_ix";
I want them to be true constants because I use them in comments.
How do I make it work? (on scala 2.11)
I want to
interface MyConstants { String TABLENAME = "table_name"; String FIELDNAME = "field_name"; String INDEXNAME = TABLENAME + "_" + FIELDNAME + "_ix"; }
but in scala. Scalac does not select constants for use in annotations if it compiles them from the java class / interface ( see SI-5333 ), so I decided to put them in a scala object. It works for literals and for expressions with literals, but not for expressions with other constants.
With this code:
import javax.persistence.Entity import javax.persistence.Table import org.hibernate.annotations.Index object MyConstants { final val TABLENAME = "table_name"; final val FIELDNAME = "field_name"; final val INDEXNAME = TABLENAME + "_" + FIELDNAME + "_ix"; } @Entity @Table(name = MyConstants.TABLENAME) @org.hibernate.annotations.Table( appliesTo = MyConstants.TABLENAME, indexes = Array(new Index(name = MyConstants.INDEXNAME, columnNames = Array(MyConstants.FIELDNAME)))) class MyEntity { }
I get the following error in the line indexes = ...
the annotation argument must be constant; found: MyConstants.INDEXNAME
Edit: After you started with several build configurations, I think this is really a problem with scala -ide. The code really compiles when I create a project using gradle or sbt. I use the build tool for my real projects, so in the end it has some obscure markers in the IDE - annoying, but has little to do with functionality.
source share