Skip to content

Annotation values can not be val's #4089

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
scabug opened this issue Dec 15, 2010 · 3 comments
Closed

Annotation values can not be val's #4089

scabug opened this issue Dec 15, 2010 · 3 comments
Assignees

Comments

@scabug
Copy link

scabug commented Dec 15, 2010

I've got an annotation in a Java file:

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target( { ElementType.PACKAGE, ElementType.TYPE })
@Documented 
@Inherited
public @interface Anno {
    String action();
    String[] roles() default "";
    String[] deny() default "";
}

And some Scala code that uses the annotations:

object Action {
  val RENDER = "RENDER"
}
object Roles {
  val ADMIN = "ADMIN"
}

object Test {
  @Anno(action = "RENDER", roles = Array ( "ADMIN" ) )
  protected final class Foo

  @Anno(action = Action.RENDER, roles = Array ( Roles.ADMIN ) )
  protected final class Bar
}

After compiling the Java file, I compile the Scala file and
get the following error messages:

Test.scala:13: error: annotation argument needs to be a constant; found: Action.RENDER
@anno(action = Action.RENDER, roles = Array ( Roles.ADMIN ) )
^
Test.scala:13: error: annotation argument needs to be a constant; found: Roles.ADMIN
@anno(action = Action.RENDER, roles = Array ( Roles.ADMIN ) )
^
two errors found

Note that the first use of the annotation is Ok, but the
second use where the values are from a val, are flagged as
being not constant.

=== What versions of the following are you using? ===

  • Scala: 2.8.1.final
  • Java:1.6.0_22 64 bit
  • Operating system: Linux of course
@scabug
Copy link
Author

scabug commented Dec 15, 2010

Imported From: https://issues.scala-lang.org/browse/SI-4089?orig=1
Reporter: Richard Emberson (rmemberson)

@scabug
Copy link
Author

scabug commented Dec 20, 2010

@hubertp said:
Seems like a duplicate of #2764 to me, but I will let Lukas to decide.

@scabug
Copy link
Author

scabug commented Mar 16, 2011

@lrytz said:
You need to add the final modifier to the value definitions in the scala objects.

From the specification:

A constant value definition is of the form
[[BR]]
final val x = e
[[BR]]
where e is a constant expression (�6.24). The final modifier must be present and no type annotation may be given.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants