11

I have an Java-Annotation that return a double value:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface DoubleValue {
  double value();
} 

When i try to attach the annotation to a field in a scala class and the value is negativ like here:

class Test {
  @DoubleValue(-0.05)
  var a = _
}

i get an compiler error with the message: "annotation argument needs to be a constant; found: 0.05.unary_-". I understood that i need a numerical literal and i looked into the Scala Language Specification and it seems, that the - sign is only used for the exponent but not for the mantissa. Does someone has an idea how i can have a negative value as runtime information using annotations?

Thanks, Klinke

Klinke
  • 207
  • 1
  • 7
  • 2
    It looks like you may have found the dark side of operators as methods. :) – Matthew Flaschen Jun 02 '10 at 15:55
  • Are you using Scala 2.7? Annotation handling in 2.8 is much improved. – Randall Schulz Jun 02 '10 at 16:13
  • *Update:* Apparently this "feature" remains in 2.8. I just tested it. – Randall Schulz Jun 02 '10 at 16:19
  • 2
    By the way, this is not a general issue of negative floating-point constants, just their use in annotation arguments. And integers are OK even now. – Randall Schulz Jun 02 '10 at 16:28
  • 2
    So, you've found a bug. I have entered it in the Scala bug database: https://lampsvn.epfl.ch/trac/scala/ticket/3521 - If you find bugs then please report them, otherwise the Scala developers won't know and the bug won't be fixed. – Jesper Jun 02 '10 at 17:50
  • 1
    @Jasper: the Scala bug database itself says, "Are you certain it's a bug, and not simply your imperfect understanding of the language? If you have any doubt, please ask somewhere else before opening a ticket". – Matt R Jun 02 '10 at 20:16
  • @Matt Yes but in this case it's quite clear that it's a bug. – Jesper Jun 03 '10 at 06:50
  • @Jesper: (sorry about the above name typo). The problem is that sometimes things that seem to be obvious bugs turn out not to be, or are known and accepted limitations etc. I guess my point is that it's not really fair to give someone a hard time about asking on Stack Overflow first rather than immediately raising an issue -- particularly when that's what the Scala developers ask people to do. – Matt R Jun 03 '10 at 11:15
  • @Matt It was not just meant for the original question asker, but also for Randall who I know knows quite a lot about Scala, and he knows that this is a bug. I agree that you should verify it first if you're not sure. – Jesper Jun 03 '10 at 11:40

1 Answers1

7

It appears that this is a bug.

Until the bug is fixed, you can take advantage of the fact that arithmetic on a constant is a constant and use

@DoubleValue( 0-0.05 )
Rex Kerr
  • 166,841
  • 26
  • 322
  • 407