Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Type aliases for annotations

In many points of my code, three annotations appears together:

@BeanProperty
@(SpaceProperty @beanGetter)(nullValue="0")

where nullValue="0" is a parameter to the annotation SpaceProperty.

Is it possible to define a single type alias for @BeanProperty @(SpaceProperty @beangetter) ?

The best I could do was:

type ScalaSpaceProperty = SpaceProperty @beanGetter

@BeanProperty
@(ScalaSpaceProperty)(nullValue = "0")

Is it possible to define a type alias for two annotations where the parameters are applied to the last one?

like image 692
Edmondo1984 Avatar asked Nov 09 '12 14:11

Edmondo1984


People also ask

What is type alias in Scala?

A type alias is usually used to simplify declaration for complex types, such as parameterized types or function types. Let's explore examples of those aliases and also look at some illegal implementations of type aliases.

What are type aliases?

Type aliases Type aliases provide alternative names for existing types. If the type name is too long you can introduce a different shorter name and use the new one instead. It's useful to shorten long generic types. For instance, it's often tempting to shrink collection types: typealias NodeSet = Set<Network.

What does <: mean in Scala?

It means an abstract type member is defined (inside some context, e.g. a trait or class), so that concrete implementations of that context must define that type. However, there is a constraint that this type ( Currency ) must actually be a subtype of AbstractCurrency .

What is classOf in Scala?

A classOf[T] is a value of type Class[T] . In other words, classOf[T]: Class[T] . For example: scala> val strClass = classOf[String] strClass: Class[String] = class java. lang. String scala> :t strClass Class[String]


2 Answers

No. You can write a macro to do this in Scala 2.10, I think (but the documentation isn't available at the moment, so I can't check).

like image 106
Alexey Romanov Avatar answered Oct 14 '22 16:10

Alexey Romanov


The only example of type aliasing annotations I know is in Scaladoc. Below follows the relevant part:

object ScalaJPA {
  type Id = javax.persistence.Id @beanGetter
}
import ScalaJPA.Id
class A {
  @Id @BeanProperty val x = 0
}

This is equivalent to writing @(javax.persistence.Id @beanGetter) @BeanProperty val x = 0 in class A.

type declarations can only deal with types. In other words you can't provide instance information in type aliases.

One alternative is to try to extend the annotation. Below I created an hypothetical SpaceProperty for illustrative purposes:

scala> import scala.annotation._; import scala.annotation.target._; import scala.reflect._;
import scala.annotation._
import scala.annotation.target._
import scala.reflect._

scala> class SpaceProperty(nullValue:String="1",otherValue:Int=1) extends Annotation with StaticAnnotation

scala> class SomeClass(@BeanProperty @(SpaceProperty @beanGetter)(nullValue="0") val x:Int)
defined class SomeClass

scala> class NullValueSpaceProperty extends SpaceProperty(nullValue="0")
defined class NullValueSpaceProperty

scala> class SomeClassAgain(@BeanProperty @(NullValueSpaceProperty @beanGetter) val x:Int)
defined class SomeClassAgain

Using the type alias:

scala> type NVSP = NullValueSpaceProperty @beanGetter
defined type alias NVSP

scala> class SomeClassAgain2(@BeanProperty @NVSP val x:Int)defined class SomeClassAgain2

There one small problem with this solution. Annotations defined in Scala couldn't be retained during runtime. So if you need to use the annotation during runtime, you may need to do the extension in Java. I say may because I am not sure if this limitation already has been amended.

like image 39
pedrofurla Avatar answered Oct 14 '22 17:10

pedrofurla