Created
October 3, 2013 10:55
-
-
Save laughedelic/6808000 to your computer and use it in GitHub Desktop.
Constructing union type from an HList
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
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
Simple example of usage: