2013-04-12 63 views
8

源代码的文本已经存在或是否有可能有一个斯卡拉宏,让我访问源的文本?比如我想这样写代码:宏访问在运行时

val list = List(1, 2, 3) 
val (text, sum) = (list.sum).withSource{(source, sum) => (source, sum)} 
// would return ("list.sum", 6) 
(list.sum).withSource{(source, sum) => println(s"$source: $sum"} 
// prints list.sum: 6 
+1

我刚刚发现有类似的功能(及以上)在以下库中:https://github.com/retronym/macrocosm;看'log','trace','desugar' ... – huynhjl

回答

8

你真的想要一个源代码或Tree够吗?

对于Tree你可以使用的Contextprefix这样的:

import scala.language.experimental.macros 
import reflect.macros.Context 

implicit class WithSourceHelper[T](source: T) { 
    def withSource[R](f: (String, T) => R): R = macro withSourceImpl[T, R] 
} 

def withSourceImpl[T, R](c: Context)(f: c.Expr[(String, T) => R]): c.Expr[R] = { 
    import c.universe.{reify, Apply} 

    val source = c.prefix.tree match { 
    case Apply(_, List(s)) => s 
    case _ => c.abort(c.enclosingPosition, "can't find source") 
    } 

    reify{ f.splice.apply(c.literal(source.toString).splice, c.Expr[T](source).splice) } 
} 

用法:

scala> val (x, y) = (1, 2) 
x: Int = 1 
y: Int = 2 

scala> {x + y}.withSource{ (s, r) => s"$s = $r" } 
res15: String = x.+(y) = 3 

scala> val list = List(1, 2, 3) 
list: List[Int] = List(1, 2, 3) 

scala> val (text, sum) = (list.sum).withSource{(source, sum) => (source, sum)} 
text: String = list.sum[Int](math.this.Numeric.IntIsIntegral) 
sum: Int = 6 

scala> (list.sum).withSource{(source, sum) => println(s"$source: $sum")} 
$line38.$read.$iw.$iw.$iw.list.sum[Int](math.this.Numeric.IntIsIntegral): 6 
+1

源代码是最好的,因为这是我写的。但是树的顶层可能已经足够好了。 – huynhjl

+0

似乎不可能删除'implicit'关键字,而是创建'WithSourceHelper'的隐式子类,该子类有一个引用'withSource'的方法......你知道是否有限制吗?我希望将宏观事物保持在最低限度,并能够提供'withSource'封装。例如我有这样的想法:'隐式类LogValue [T](t:T)扩展WithSourceHelper(t){def logvalue = withSource {(s,t)=> {println(s); t}}' – huynhjl

+0

我显然需要24小时才能给你赏金...... – huynhjl

1

我是不是能够直接重用withSource只是打印源和值,并返回值。该withSource宏不能从同一个对象本身使用的(所以我不能只是添加我withSource的略加修改就在该文件),我不能够调用withSourceWithSourceHelper一个子类,通过继承限制重用。

如果有人有兴趣,这里是一个补充塞尼亚的答案,只需登录与源的值,并返回值,因此计算的其余部分可发生。

def logValueImpl[T](c: Context): c.Expr[T] = { 
    import c.universe._ 
    val source = c.prefix.tree match { 
    case Apply(_, List(s)) => s 
    case _ => c.abort(c.enclosingPosition, "can't find source") 
    } 
    val freshName = newTermName(c.fresh("logValue$")) 
    val valDef = ValDef(Modifiers(), freshName, TypeTree(source.tpe), source) 
    val ident = Ident(freshName) 
    val print = reify{ 
    println(c.literal(show(source)).splice + ": " + c.Expr[T](ident).splice) } 
    c.Expr[T](Block(List(valDef, print.tree), ident)) 
} 

然后,我将其定义为def p = macro Debug.logValueImpl[T]上的隐式转换。然后我可以使用这样的:

List(1, 2, 3).reverse.p.head 
// prints: immutable.this.List.apply[Int](1, 2, 3).reverse: List(3, 2, 1) 

有趣的是,我可以申请两次:

List(1, 2, 3).reverse.p.p 

,它会告诉我logValueImpl宏做了什么:

{ 
    val logValue$7: List[Int] = immutable.this.List.apply[Int](1, 2, 3).reverse; 
    Predef.println("immutable.this.List.apply[Int](1, 2, 3).reverse: ".+(logValue$7)); 
    logValue$7 
} 

这似乎与其它宏以及工作:

f"float ${1.3f}%3.2f; str ${"foo".reverse}%s%n".p` 
//prints: 
{ 
    val arg$1: Float = 1.3; 
    val arg$2: Any = scala.this.Predef.augmentString("foo").reverse; 
    scala.this.Predef.augmentString("float %3.2f; str %s%%n").format(arg$1, arg$2) 
}: float 1.30; str oof%n 

更有趣的是,如果我用showRaw代替show我甚至可以看到扩展的宏,这可能会派上用场弄清楚如何写其他宏树。