Type Parameters on Scala Macro Annotations

Imran Rashid

I'm trying to use macro annotations in scala, where my macro annotation would take an argument of another type. It would then use scala reflection to look at the passed in type, and add some methods as appropriate.Eg.

trait MyTrait {
  def x: Int
  def y: Float
}

@MyAnnotation class MyClass //<-- somehow, this annotation should reference MyTrait

class MyAnnotation(val target: Any) extends StaticAnnotation {
  def macroTransform(annottees: Any*) = macro MyAnnotationImpl.impl
}
object MyAnnotationImpl {
  def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
    // if I can get a handle on the type MyTrait in here
    // then I can call .members on it, etc.
    ...
  }
}

Basically, the same thing as Using Scala reflection in Scala macros, except using macro annotations. However, when I try to template my macro annotation with a TypeTag

class MyAnnotation[T](val target: Any) extends StaticAnnotation {
  def macroTransform[T](annottees: Any*) = macro MyAnnotationImpl.impl[T]
}
object MyAnnotationImpl {
  def impl[T: c.WeakTypeTag](c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
    ...
  }
}

I get

[error] /Users/imran/other_projs/learn_macros/macros/src/main/scala/com/imranrashid/oleander/macros/MacrosWithReflection.scala:7: macro annotation has wrong shape:
[error]   required: def macroTransform(annottees: Any*) = macro ...
[error]   found   : def macroTransform[T](annottees: Any*) = macro ...
[error] class MyAnnotation[T](val target: Any) extends StaticAnnotation {
[error]       ^

I've also tried to make the type an argument to my annotation, so I would use it like @MyAnnotation(MyTrait) class Foo .... I can extract the name as a String with something like

val targetTrait = c.prefix.tree match {
  case Apply(Select(New(Ident(_)), nme.CONSTRUCTOR), List(Ident(termName))) => termName
}

but, I'm not sure what I can do w/ that String to get back the full type. I've also tried variants like @MyAnnotation(typeOf[MyTrait]) class Foo ..., and then use c.eval on the typeOf inside my macro, but that doesn't compile either.

Eugene Burmako

In macro paradise 2.0.0-SNAPSHOT we have quite a tricky way of accessing type parameters for macro annotations (the situation will improve later on when we have dedicated APIs for that, but right now it's very difficult to introduce new functionality to scala-reflect.jar in macro paradise, so the current API is a bit rough).

For now it's necessary to specify the type parameter on the annotation class and not to declare any type parameters on the macroTransform method. Then, in macro expansion, access c.macroApplication and extract the untyped tree corresponding to the passed type parameter. Afterwards, do c.typeCheck as described in Can't access Parent's Members while dealing with Macro Annotations.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Type Parameters on Scala Macro Annotations

From Dev

Passing type parameter to scala meta macro/annotations

From Dev

Scala macro annotation - case class with type parameters

From Dev

Scala macro annotation - case class with type parameters

From Dev

Scala Macro Annotations: c.TypeCheck of annotated type causes StackOverflowError

From Dev

Macro annotations and type parameter

From Dev

Macro annotations and type parameter

From Dev

Scala Pickling and type parameters

From Dev

Scala - Method type parameters

From Dev

Scala type alias with type parameters

From Dev

Link annotations on type parameters in subtypes to declaring type parameters in supertype

From Dev

How can parameters/settings be passed to a Scala macro?

From Dev

Handling by-name parameters in Scala macro

From Dev

Getting Parameters from Scala Macro Annotation

From Dev

Type annotations for class parameters and return values

From Dev

Scala Cake Pattern & Self Type Annotations

From Dev

Scala: question marks in type parameters

From Dev

Scala - extract Either type parameters

From Dev

Infering generic type parameters in Scala

From Dev

Scala type parameters in various places

From Dev

Scala nested implicit type parameters

From Dev

How to construct wildcard type in Scala macro?

From Dev

How to construct wildcard type in Scala macro?

From Dev

Class type required but E found (scala macro)

From Dev

How to access type annotations on a receiver type's parameters

From Dev

Scala infix type aliasing for >2 type parameters?

From Dev

Scala type parameters by declaring the type inside the function

From Dev

Scala infix type aliasing for >2 type parameters?

From Dev

Apart from macro annotations, what macro creation methods avoid immediate type checking?

Related Related

  1. 1

    Type Parameters on Scala Macro Annotations

  2. 2

    Passing type parameter to scala meta macro/annotations

  3. 3

    Scala macro annotation - case class with type parameters

  4. 4

    Scala macro annotation - case class with type parameters

  5. 5

    Scala Macro Annotations: c.TypeCheck of annotated type causes StackOverflowError

  6. 6

    Macro annotations and type parameter

  7. 7

    Macro annotations and type parameter

  8. 8

    Scala Pickling and type parameters

  9. 9

    Scala - Method type parameters

  10. 10

    Scala type alias with type parameters

  11. 11

    Link annotations on type parameters in subtypes to declaring type parameters in supertype

  12. 12

    How can parameters/settings be passed to a Scala macro?

  13. 13

    Handling by-name parameters in Scala macro

  14. 14

    Getting Parameters from Scala Macro Annotation

  15. 15

    Type annotations for class parameters and return values

  16. 16

    Scala Cake Pattern & Self Type Annotations

  17. 17

    Scala: question marks in type parameters

  18. 18

    Scala - extract Either type parameters

  19. 19

    Infering generic type parameters in Scala

  20. 20

    Scala type parameters in various places

  21. 21

    Scala nested implicit type parameters

  22. 22

    How to construct wildcard type in Scala macro?

  23. 23

    How to construct wildcard type in Scala macro?

  24. 24

    Class type required but E found (scala macro)

  25. 25

    How to access type annotations on a receiver type's parameters

  26. 26

    Scala infix type aliasing for >2 type parameters?

  27. 27

    Scala type parameters by declaring the type inside the function

  28. 28

    Scala infix type aliasing for >2 type parameters?

  29. 29

    Apart from macro annotations, what macro creation methods avoid immediate type checking?

HotTag

Archive