Skip to content

Instantly share code, notes, and snippets.

@gigiigig
Last active November 11, 2015 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gigiigig/91f182024778976ee371 to your computer and use it in GitHub Desktop.
Save gigiigig/91f182024778976ee371 to your computer and use it in GitHub Desktop.
Check if a case class contain another.
import shapeless._
import shapeless.record._
import shapeless.ops.hlist.Selector
object console extends App {
trait HListContains[T <: HList, R <: HList]
object HListContains {
implicit def hnil[T <: HList]: HListContains[T, HNil] =
new HListContains[T, HNil] {}
implicit def hlist[HH, T <: HList, TT <: HList]
(implicit s: Selector[T, HH],
c: HListContains[T, TT]): HListContains[T, HH :: TT] =
new HListContains[T, HH :: TT] {}
}
trait Contains[A, B]
object Contains {
implicit def apply[A, B, R <: HList, S <: HList, Res](
implicit ga: LabelledGeneric.Aux[A, R],
gb: LabelledGeneric.Aux[B, S],
hlc: HListContains[R, S]) = new Contains[A, B] {}
}
case class Foo(b: Option[Boolean], i: Int)
case class Bar(i: Int, s: String, b: Option[Boolean])
case class Empty()
def foo[A, B](a: A, b: B)(implicit c: Contains[A, B]) = ()
foo(Foo(Some(false), 1), Empty())
foo(Bar(1, "", Some(false)), Foo(Some(true), 1))
//foo(Foo(1), Bar(1, ""))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment