Question

I would like to create an annotation or trait that adds methods to an object at compile time dynamically, based on existing fields. Although I'm interested in something at the class-level, I'd also work with field-level annotations (or something else more granular) as well.

An older stack-overflow question asking about the implementation details of Scala's @BeanProperty was answered with, "It's a compiler plugin, but macros may also allow you to do this". Given the official (if experimental) release of macros in Scala 2.10, is this sort of functionality now possible?

Was it helpful?

Solution

Update: This answer is not valid anymore. See Eugenes comment.

No, it is not yet possible.

In 2.10 there exists only def macros that can't do anything comparable. For 2.11 the world is a bit better, macro annotations and an implementation to introduce members to classes already exists. But they are only some weeks old and therefore will work only for some corner cases. Furthermore the implementation to introduce members to classes lives in a different branch than the implementation for macro annotations, thus it is not yet possible to use them together.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top