Type Parameters on Scala Macro Annotations

后端 未结 2 1273
没有蜡笔的小新
没有蜡笔的小新 2020-12-18 09:01

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

2条回答
  •  粉色の甜心
    2020-12-18 09:53

    As Eugene points out in his answer it is possible to match on the tree of the whole macro application. Like every Scala method, annotation macro applications can take multiple type argument lists as well as multiple value argument lists.

    Consider the macro application of an annotation macro called test:

        @test[A, B][C, D](a, b)(c, d) trait Foo
    

    In the implementation of test we can inspect the macro application by

        println(show(c.macroApplication))
    

    which will result in:

        new test[A, B][C, D](a, b)(c, d).macroTransform(abstract trait Foo extends scala.AnyRef)
    

    To extract the (type/value) parameters from the tree you have to pattern match on the tree. A parser for an arbitrary amount of parameter lists can be found in this project

    Using this parser retrieving the first value argument of the macro application is as easy as

        val List(List(arg)) = MacroApp(c.macroApplication).termArgs
    

提交回复
热议问题