Skip to content

Instantly share code, notes, and snippets.

@DavidDudson
Created August 9, 2016 00:11
Show Gist options
  • Save DavidDudson/b2fb2f51c8919a1584911dc5bd5c1d61 to your computer and use it in GitHub Desktop.
Save DavidDudson/b2fb2f51c8919a1584911dc5bd5c1d61 to your computer and use it in GitHub Desktop.
@WhatAmI
object Test {
@WhatAmI
def main(args: Array[String]) = {}
@WhatAmI
val hi = ""
@WhatAmI
var hello = ""
@WhatAmI
object SomeObject {}
@WhatAmI
class SomeClass(a:String) {}
@WhatAmI
case class SomeCaseClass(a:String) {}
@WhatAmI
trait SomeTrait
@WhatAmI
sealed trait SomeSealedTrait
}
import scala.annotation.compileTimeOnly
import scala.meta._
@compileTimeOnly("@WhatAmI not expanded")
class WhatAmI extends scala.annotation.StaticAnnotation {
inline def apply(defn: Defn) = meta {
defn match {
case _:Defn.Def => println("Def")
case _:Defn.Class => println("Class")
case _:Defn.Macro => println("Macro")
case _:Defn.Trait => println("Trait")
case _:Defn.Type => println("Type")
case _:Defn.Val => println("Val")
case _:Defn.Var => println("Var")
case _:Defn.Object => println("Object")
case _=> println("Unknown")
}
defn
}
}
@DavidDudson
Copy link
Author

DavidDudson commented Aug 9, 2016

1.Compile:
2. Observe that it only prints Object
3. Remove WhatAmI in line 1 of Test.scala
4. Compile
5. Observe that the list of annotations are printed correctly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment