Scala-style Runtime-visible Annotations?

463 views
Skip to first unread message

Adam Jorgensen

unread,
Apr 18, 2011, 1:22:22 AM4/18/11
to scala-l...@googlegroups.com
So, does anyone know if any progress has been made with implementing
runtime-visible scala-style
annotations?

I know it's possible to get runtime visibility using annotations
implemented in Java but this is not ideal
as the Java annotation definition system is a lot more kludgy than
Scala-style annotations.

I found this sad, lonely ticket on the Scala Trac system:
https://lampsvn.epfl.ch/trac/scala/ticket/32

It looks to have been abandoned...

Is there any hope that we will get this feature implemented at some
point in time...


Of course, if anyone knows of a better feature that can replace the
needs for this one that works
right now I would like to hear about it :-)

Lukas Rytz

unread,
Apr 18, 2011, 1:59:59 AM4/18/11
to scala-l...@googlegroups.com
On Mon, Apr 18, 2011 at 07:22, Adam Jorgensen <adam.jorgensen.za@gmail.com> wrote:
So, does anyone know if any progress has been made with implementing
runtime-visible scala-style
annotations?

I know it's possible to get runtime visibility using annotations
implemented in Java but this is not ideal
as the Java annotation definition system is a lot more kludgy than
Scala-style annotations.

I found this sad, lonely ticket on the Scala Trac system:
https://lampsvn.epfl.ch/trac/scala/ticket/32

It looks to have been abandoned...

It has low priority, and nobody is working on this AFAICT. If somebody
steps up and starts implementing we'll be happy to accept patches..

Ismael Juma

unread,
Apr 18, 2011, 4:13:39 AM4/18/11
to scala-l...@googlegroups.com
On Mon, Apr 18, 2011 at 6:59 AM, Lukas Rytz <lukas...@epfl.ch> wrote:
> It has low priority, and nobody is working on this AFAICT. If somebody
> steps up and starts implementing we'll be happy to accept patches..

It's worth mentioning that this makes the following more of an issue.

https://lampsvn.epfl.ch/trac/scala/ticket/2764

Since one has to use Java for runtime annotations, one ends up using
features like Java static constants or Java enums while creating them
in the same project as the Scala files to avoid having a separate
project just for the annotations. And then one runs into #2764.

Best,
Ismael

Adam Jorgensen

unread,
Apr 18, 2011, 9:16:50 AM4/18/11
to scala-l...@googlegroups.com
Ouch, nasty.

It seems like it would be an all-round good think of runtime visible
annotations were implemented then. It's a pity that the ticket
in question has low priority.

Does anyone have an idea of how much work would be involved
in implementing this feature?

Lukas Rytz

unread,
Apr 18, 2011, 10:00:56 AM4/18/11
to scala-l...@googlegroups.com
On Mon, Apr 18, 2011 at 15:16, Adam Jorgensen <adam.jorgensen.za@gmail.com> wrote:
Ouch, nasty.

It seems like it would be an all-round good think of runtime visible
annotations were implemented then. It's a pity that the ticket
in question has low priority.

Does anyone have an idea of how much work would be involved
in implementing this feature?

It's definitely non-trivial. Java annotations have specific representation in
bytecode (the classfile format described here), which would require changes
to Scala's bytecode generator.

Also the language of java annotations is quite special, different from the rest of
the Java language (see here), so one would need to define how this can be
represented in a Scala class.

Lukas

Adam Jorgensen

unread,
Apr 18, 2011, 3:46:01 PM4/18/11
to scala-l...@googlegroups.com
On 18 April 2011 16:00, Lukas Rytz <lukas...@epfl.ch> wrote:
>
>
> On Mon, Apr 18, 2011 at 15:16, Adam Jorgensen <adam.jor...@gmail.com>

> wrote:
>>
>> Ouch, nasty.
>>
>> It seems like it would be an all-round good think of runtime visible
>> annotations were implemented then. It's a pity that the ticket
>> in question has low priority.
>>
>> Does anyone have an idea of how much work would be involved
>> in implementing this feature?
>
> It's definitely non-trivial. Java annotations have specific representation
> in
> bytecode (the classfile format described here), which would require changes
> to Scala's bytecode generator.
> Also the language of java annotations is quite special, different from the
> rest of
> the Java language (see here), so one would need to define how this can be
> represented in a Scala class.
> Lukas

Indeed, very non-trivial. It's a real pity this is not something that can
be implemented easily because it's a useful feature :-/

So the difficulty really lies in the fact that Java annotations are one of
those "special cases" that scala tries to avoid :-)

Again, most unfortunate because Scala annotations are a lot more logical
and consistent than Java ones...

Micael Pedrosa

unread,
Aug 10, 2012, 11:45:16 AM8/10/12
to scala-l...@googlegroups.com
Why not to avoid annotation bytecode at all.
It seams that a similar situation is already done for class static methods.
Wrap scala annotations as simple class objects.

I'm also very interested in this feature, since I'm pursuing the potential of scala annotations in java for a long time.
It'll be interesting to do something like this in scala:

//define annotation with functional validator and error message
class Validation[A](code: A => Boolean, error: String) extends Annotation

//use validator in field
class Entity(
@Validation[String](_.contains("@gmail.com"), msg="Not a google email")
var gEmail: String
)

Micael Pedrosa

unread,
Aug 10, 2012, 12:02:36 PM8/10/12
to scala-l...@googlegroups.com
Could be even better if it has some kind of implicit type to the object that is annotating.
So a short of could be made:

@Validation(_.contains("@gmail.com"), msg="Not a google email")

instead of
Reply all
Reply to author
Forward
0 new messages