Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i have a negative value as constant expression in Scala?

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

like image 774
Klinke Avatar asked Jun 02 '10 15:06

Klinke


1 Answers

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 )
like image 84
Rex Kerr Avatar answered Sep 19 '22 06:09

Rex Kerr