13

I just realized I cannot have annotations in scala, that are preserved and analyzed at runtime. I also checked this question, but I didn't quite get it what are the alternatives.

  • DI - an answer mentions that there is no need for DI framework in scala. While that might be the case on a basic level (although I didn't quite like that example; what's the idiomatic way of handling DI?), Java DI frameworks like spring are pretty advanced and handle many things like scheduled jobs, caching, managed persistence, etc, all through annotations, and sometimes - custom ones.

  • ORM - I'll admit I haven't tried any native scala ORM, but from what I see in squeryl, it also makes some use of annotations, meaning they are unavoidable?

  • any serialization tool - how do you idiomatically customize serialization output to JSON/XML/...?

  • Web service frameworks - how do you define (in code) the mappings, headers, etc. for RESTful or SOAP services?

Scala users need to have a hybrid scala/java (for the annotations) project in order to use these facilities that are coming from Java?

And are the native scala alternatives for meta-data nicer than annotations? I'm not yet fully into the scala mode of thinking, and therefore most of the examples look ugly to me, compared to using annotations, so please try to be extra convincing :)

Community
  • 1
  • 1
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140

1 Answers1

9

Actually, Scala does have runtime-retained annotations. The difference is that they are not stored as Java annotations but are instead encoded inside the contents of binary ScalaSignature annotation (which, by itself, is a runtime-retained Java annotation).

So, Scala annotations can be retrieved at runtime, but instead of using Java reflection, one must use Scala reflection:

class Awesome extends StaticAnnotation

@Awesome
class AwesomeClass

import scala.reflect.runtime.universe._

val clazz = classOf[AwesomeClass]
val mirror = runtimeMirror(clazz.getClassLoader)
val symbol = mirror.classSymbol(clazz)
println(symbol.annotations) // prints 'List(Awesome)'

Unfortunately, Scala reflection is still marked as experimental and is actually unstable at this point (SI-6240 or SI-6826 are examples of quite serious issues). Nevertheless, it seems like the most straightforward replacement for Java reflection and annotations in the long term.

As for now, one has to use Java annotations which I think is still a nice solution.

Regarding frameworks and libraries for DI/ORM/WS/serialization - Scala still doesn't seem to be mature in this area, at least not as Java is. There are plenty of ongoing projects targeting these problems, some of them are already really nice, others are still in development. To name a few that come to my mind: Squeryl, Slick, Spray, Pickling.

Also, Scala has some advanced features that often make annotations unneccessary. Typeclasses (implemented with implicit parameters) are probably good example of that.

ghik
  • 10,706
  • 1
  • 37
  • 50
  • http://stackoverflow.com/questions/2265773/how-do-you-define-an-interface-in-scala says StaticAnnotation is not preserved at runtime. Does that mean for the "java runtime"? – Bozho Aug 27 '13 at 20:50
  • @Bozho I think so. This is a question from three years ago when Scala reflection did not yet exist. – ghik Aug 27 '13 at 20:56