Skip to content

Instantly share code, notes, and snippets.

@takezoe
Forked from xuwei-k/NotVar.scala
Created April 28, 2012 15:09
Show Gist options
  • Save takezoe/2519721 to your computer and use it in GitHub Desktop.
Save takezoe/2519721 to your computer and use it in GitHub Desktop.
not var macro scala
def NotVar(expr: Any): Any = macro NotVarImpl
def NotVarImpl(c: Context)(expr: c.Expr[Any]): c.Expr[Any] = {
import c.mirror._
import reflect.api.Modifier.mutable
Expr((new Transformer{
override def transform(tree:Tree):Tree = tree match{
case ValDef(m,_,_,_) if m.hasModifier(mutable) => sys.error("can't use var !!!")
case _ => super.transform(tree)
}
}).transform(expr.tree))
}
@takezoe
Copy link
Author

takezoe commented Apr 28, 2012

xuwei-kさんのこれ https://gist.github.com/1900672 をScala 2.10の最近のナイトリービルドでやってみた。こんな感じ?

@takezoe
Copy link
Author

takezoe commented Apr 28, 2012

Transformerはそのままでいいのか。まあツリーを舐めるだけなら最初にやってたみたいに expr.tree.foreach { t => ... } でもいい気がするかな。

@takezoe
Copy link
Author

takezoe commented Apr 30, 2012

2.10 M3で動くように修正してみた。

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