Skip to content

Instantly share code, notes, and snippets.

@wheaties
Created October 31, 2014 02:52
Show Gist options
  • Save wheaties/0263940a7f04cb6e0da2 to your computer and use it in GitHub Desktop.
Save wheaties/0263940a7f04cb6e0da2 to your computer and use it in GitHub Desktop.
Type Contains
sealed trait TList
case object TNil extends TList
case class <+>[A, B <: TList]() extends TList //some weird symbol for ::
trait Contains[A, TL <: TList]
object Contains extends LowPriorityContains{
def apply[A, TL <: TList](implicit cnt: Contains[A, TL]) = cnt
implicit def has[F[_], A, T <: TList] = new Contains[F, F[A] <+> T]{}
implicit def has[F[_], H, T <: TList](implicit cnt: Contains[F, T]) = new Contains[F, H :: T]{}
}
trait LowPriorityContains{
implicit def has[A, T <: TList] = new Contains[A, A <+> T]{}
implicit def recur[A, H, T <: TList](implicit cnt: Contains[A, T]) = new Contains[A, H <+> T]{}
}
//Next question, does it work on higher kinded types? List[_]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment