Last active
April 16, 2018 08:31
-
-
Save n4to4/f5e4a68f389946474317c2f292a431dd to your computer and use it in GitHub Desktop.
enum.scala // https://stackoverflow.com/questions/25838411/cant-prove-that-singleton-types-are-singleton-types-while-generating-type-class
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
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