Skip to content

Instantly share code, notes, and snippets.

@n4to4
Last active April 16, 2018 08:31
Show Gist options
  • Save n4to4/f5e4a68f389946474317c2f292a431dd to your computer and use it in GitHub Desktop.
Save n4to4/f5e4a68f389946474317c2f292a431dd to your computer and use it in GitHub Desktop.
import shapeless._
trait AllSingletons[A, C <: Coproduct] {
def values: List[A]
}
object AllSingletons {
implicit def cnilSingletons[A]: AllSingletons[A, CNil] =
new AllSingletons[A, CNil] {
def values = Nil
}
implicit def coproductSingletons[A, H <: A, T <: Coproduct](implicit
tsc: AllSingletons[A, T],
witness: Witness.Aux[H]
): AllSingletons[A, H :+: T] =
new AllSingletons[A, H :+: T] {
def values = witness.value :: tsc.values
}
}
sealed trait Foo
case object Bar extends Foo
case object Baz extends Foo
trait EnumerableAdt[A] {
def values: Set[A]
}
object EnumerableAdt {
implicit def fromAllSingletons[A, C <: Coproduct](implicit
gen: Generic.Aux[A, C],
singletons: AllSingletons[A, C]
): EnumerableAdt[A] = {
val _ = gen
new EnumerableAdt[A] {
def values = singletons.values.toSet
}
}
}
object Main extends App {
val x = implicitly[AllSingletons[Foo, Bar.type :+: Baz.type :+: CNil]]
println(x.values)
val y = implicitly[EnumerableAdt[Foo]]
println(y.values)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment