Last active
December 19, 2015 04:09
-
-
Save travisbrown/5895194 to your computer and use it in GitHub Desktop.
Macro-based heterogeneous list constructor.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.language.experimental.macros | |
import scala.reflect.macros.Context | |
import shapeless._ | |
object hlist { | |
def apply(xs: Any*) = macro apply_impl | |
def apply_impl(c: Context)(xs: c.Expr[Any]*) = { | |
import c.universe._ | |
xs.map(_.tree) match { | |
case Seq(Typed(_, Ident(tpnme.WILDCARD_STAR))) => | |
c.abort(c.enclosingPosition, "Can't use _* type ascription here!") | |
case _ => xs.foldRight[c.Expr[HList]](reify(HNil)) { | |
case (x, acc) => reify(x.splice :: acc.splice) | |
} | |
} | |
} | |
} | |
// Now `hlist(1, "a", 'a)` will have inferred type `Int :: String :: Symbol :: HNil`. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment