This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Leveraging Annotations

2 replies
Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 5 days ago.
Hey all,

First,
I want to use a "presentation-only" compiler to try to pull out some annotations (StaticAnnotations) from classes.  I was wondering where I should look in the compiler/other open-source projects for some sample code.  Also, is this approach reasonable, or is there an easier approach? 

Second,
Is there a way to have optional arguments for a scala annotation class?  I tried something like the following:

class parameter extends StaticAnnotation {
   val expression : Option[String] = None;
}

class TestMojo {
   @parameter{ override val expression = Some("${project}")
   var project: MavenProject = _
   @parameter
   var other = _
}

Any suggestions? (besides reverting to defining the annotations in java)


Thanks,
-Josh


daniel
Joined: 2008-08-20,
User offline. Last seen 44 weeks 14 hours ago.
Re: Leveraging Annotations
Just a thought, but one hack you could try to clean up your workaround would be an implicit conversion as follows:

implicit def allToSome[A](a: A) = Some(a)

On Sun, Dec 21, 2008 at 9:11 PM, Josh Suereth <joshua [dot] suereth [at] gmail [dot] com> wrote:
Hey all,

First,
I want to use a "presentation-only" compiler to try to pull out some annotations (StaticAnnotations) from classes.  I was wondering where I should look in the compiler/other open-source projects for some sample code.  Also, is this approach reasonable, or is there an easier approach? 

Second,
Is there a way to have optional arguments for a scala annotation class?  I tried something like the following:

class parameter extends StaticAnnotation {
   val expression : Option[String] = None;
}

class TestMojo {
   @parameter{ override val expression = Some("${project}")
   var project: MavenProject = _
   @parameter
   var other = _
}

Any suggestions? (besides reverting to defining the annotations in java)


Thanks,
-Josh



Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 5 days ago.
Re: Leveraging Annotations
That would be helpful, except the @parameter { override val expression = ... } syntax doesn't work.  So optional values to annotations only work if they're defined in java (as far as I can tell)

On Mon, Dec 22, 2008 at 11:48 AM, Daniel Spiewak <djspiewak [at] gmail [dot] com> wrote:
Just a thought, but one hack you could try to clean up your workaround would be an implicit conversion as follows:

implicit def allToSome[A](a: A) = Some(a)

On Sun, Dec 21, 2008 at 9:11 PM, Josh Suereth <joshua [dot] suereth [at] gmail [dot] com> wrote:
Hey all,

First,
I want to use a "presentation-only" compiler to try to pull out some annotations (StaticAnnotations) from classes.  I was wondering where I should look in the compiler/other open-source projects for some sample code.  Also, is this approach reasonable, or is there an easier approach? 

Second,
Is there a way to have optional arguments for a scala annotation class?  I tried something like the following:

class parameter extends StaticAnnotation {
   val expression : Option[String] = None;
}

class TestMojo {
   @parameter{ override val expression = Some("${project}")
   var project: MavenProject = _
   @parameter
   var other = _
}

Any suggestions? (besides reverting to defining the annotations in java)


Thanks,
-Josh




Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland