Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for use constants in scala annotations

I use tapestry 5 as my choice of web framework. Tapestry allows me to define symbols in the configure class and inject symbols into other components.

for example,

public interface SymbolConstants {
  static String DEFAULT_TIMEOUT_KEY = "default.timeout"; 
}

public class AppModule {
   void contributeApplicationDefault(Configuration conf) {
       conf.add(SymbolConstants.DEFAULT_TIMEOUT_KEY, "10");
   }
}

public class MyComponent {
  @Symbol(SymbolConstants.DEFAULT_VALUE_KEY)
  private long timeout;
}

The ability to define static constants and use them as annotation values gives me compile time check.

I am wondering how to define constants and use them as values of scala annotations. If not, what is the best practice to define/limit the value that we can assign to annotations in scala.

like image 954
Alan Ho Avatar asked Nov 02 '10 04:11

Alan Ho


1 Answers

The 'final' keyword is required to make the compiler emit it as you would do it in Java. E.g.,

object Foo
{
   final val MY_SYMBOLIC_CONSTANT="whatever"
}

It seems that, otherwise, you only get an accessor method under the hood which is not statically calculable.

like image 75
F. P. Freely Avatar answered Sep 22 '22 02:09

F. P. Freely