Skip to content

Instantly share code, notes, and snippets.

@eparejatobes
Forked from laughedelic/UnionHList.scala
Created October 3, 2013 11:48
Show Gist options
  • Save eparejatobes/6808556 to your computer and use it in GitHub Desktop.
Save eparejatobes/6808556 to your computer and use it in GitHub Desktop.
trait UnionOf[L <: HList] {
type Mix
type Out = not[Mix]
type is[O] = UnionAux[L, O]
}
/* Implicits for constructing union: */
object UnionOf {
implicit val nil =
new UnionOf[HNil] { type Mix = not[Nothing] }
implicit def last[H, T <: HNil] =
new UnionOf[H :: T] { type Mix = not[H] }
implicit def cons[H, T <: HList : shapeless.IsHCons](implicit m: UnionOf[T]) =
new UnionOf[H :: T] { type Mix = not[H] with m.Mix }
}
/* Stupid but necessary auxiliary stuff: */
trait UnionAux[L <: HList, Out]
object UnionAux {
implicit def uhlist[L <: HList](implicit u: UnionOf[L]) = new UnionAux[L, u.Out] {}
}
// Convenient aliases that will be put in scope by the package object
trait UnionHList {
type not[A] = A => Nothing
type SubtypeOf[U] = { type is[T] = not[not[T]] <:< U }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment