Macro annotation to override toString of Scala function
问题 How to write macro annotation which looks in usage like @named("+2") _ + 2 and produces: new (Int => Int) { override def toString(): String = "+2" def apply(x: Int): Int = x + 2 } 回答1: Correct syntax is ((_: Int) + 2): @named("+2") . Unfortunately macro annotations annotating expressions don't expand. The simplest is to use object Named { def build[T, R](name: String)(applyFunc: T => R): T => R = new (T => R) { override def toString() = name def apply(x: T): R = applyFunc(x) } } without any