Skip to content

Instantly share code, notes, and snippets.

@mandubian
Last active December 19, 2015 17:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mandubian/5991615 to your computer and use it in GitHub Desktop.
Save mandubian/5991615 to your computer and use it in GitHub Desktop.
// pseudo-code mixing quasiquotes with 2.10 syntax, don't worry ;)
Macros.doit( "a" -> ("b", "c"), "a1" -> ("b1", "c1") )
object Macros {
def doit(xs: Any*) = macro doitImpl
def doitImpl(c: Context)(xs: c.Expr[Any]*) = {
import c.universe._
val fields = xs.toList map {
case q"${_}(${Literal(Constant(name: String))}).->[${_}]($value)" =>
q"""
val ${name} = functionDoingSomething(.. $valueConvertedToVarArgs)
/* inspects $value, verifies it's tupleX, converts it into a varargs */
"""
}
// use fields in some generated code...
}
}
@xeno-by
Copy link

xeno-by commented Jul 13, 2013

You can both match and splice with "..$". So by writing (..$value) or (..$args) in the pattern, you'll be able to capture a list of trees at once. Then you can verify its length and splice the list back into the result using (..$args). Please let me know if something doesn't work. (Probably that'd be better to do via twitter, since I usually don't get notifications about gist comments).

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