Reputation: 2574
I want do a Log annotation use scala meta.Usage simple as:
@Log
def f1(a: Int) {
Future(a + 1) //result is a Future
}
// after parsed ====>
def f1(a: Int) {
Future(a + 1).map{x =>
println(x)
x
}
}
How can I check if f1
is a Future[ _ ]
type? Thanks
Besides, I don't want use reflection(performance cost) in Log
annotation and I think method result type can be decided at compile time.
Upvotes: 2
Views: 359
Reputation: 421
This is not possible with scalameta macro annotations. The macro annotations are syntactic so they don't have access to semantic information such as types of trees.
PS. I am not sure if a macro annotation is suitable for this use-case, you could write out the log statement explicitly with something like this
def log[T](x: T): T = { println(x); x }
Future(1).map(log)
or even more concisely with an extension method
implicit class XtensionFuture[T](future: Future[T]) {
def logged: Future[T] = future.map(log)
}
Future(1).logged
Macros should ideally be used as a last resort, not for convenience only.
Upvotes: 1