Skip to content

Instantly share code, notes, and snippets.

@travisbrown
Last active December 19, 2015 04:09
Show Gist options
  • Save travisbrown/5895194 to your computer and use it in GitHub Desktop.
Save travisbrown/5895194 to your computer and use it in GitHub Desktop.
Macro-based heterogeneous list constructor.
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