dimanche 7 février 2016

What is the (current) state of scala reflection capabilities, especially wrt annotations, as of version 2.11?

scala appears to be a wonderful addition to the JVM universe. It reminds me of a strange hybrid of C++, C#, and Swift, nested in the JVM world.

However, many of scala's features may be inaccessible due to lacking or out-of-date documentation.

This seems especially true with respect to its reflection capabilities.

For instance, I am assessing whether or not it would be possible to augment scala classes at either runtime or compiletime using scala annotations. I am using the latest scala version 2.11. As a motivating example, let's say I make a case class SimpleAnnotation() extends StaticAnnotation. I would like to, at runtime, find all case classes with that annotation.

This is probably the most typical and vanilla use case for annotations.

In C# and in Java it is relatively straightforward to determine at runtime whether a given class is annotated. This is a canonical sort of use case with a canonical sort of answer. Yet in scala it is unclear to me what I ought to do to achieve this behavior, or even whether it is possible. In particular, after scanning some previous material on scala annotations and reflection, I am left wondering:

  • Is this possible?
  • Is this only possible at runtime or complile time?
  • Is this only possible before or after scala version 2.10?
  • Is this only possible using Java annotations on scala classes?
  • Why does getClass[AnnotatedClass].getAnnotations return such seemingly garbled information?
  • Why are macros and reflection seemingly conflated in scala?

Any guidance is appreciated... and I'm sure I am not the only one who is confused.





Aucun commentaire:

Enregistrer un commentaire