Another Tour of Scala

ScalaAnnotations

The Gist

Annotations gives a pretty decent overview of how annotations are used in Scala.

My Interpretations

Annotations in Scala are conceptually the same as they are in Java. Many of the Java keyword modifiers are annotations in Scala (as opposed to language constructs). For example, native in Java is the annotation scala.native.

For the purposes of interacting with Java, Scala classes may annotate methods with the throws annotation. This is the equivalent of the throws keyword in Java and lets the Java compiler know what exceptions must be caught.

Java Annotations, when used in Scala, are specified using one of two types of syntax:

One way in which Scala uses annotations is to bridge Scala’s way of dealing with properties and Java’s way:

The BeanProperty annotation basically creates these methods for us. This lets Scala code work with systems like Spring (that expect the get/set style) without having duplicated code in our Scala source.

My Thoughts on this Feature

I’ve never felt like annotations were a particularly good addition to Java; they always seemed like a hack to support poorly designed frameworks like J2EE and JAX-RS. I don’t know how endemic they are to Scala at this time, so lets hope they don’t get used much. Scala is a much more expressive language than Java and I think it can get by without annotations being used all over the place.

Also, the use of parens vs. braces seems needlessly inconsistent.