Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Created February 21, 2021 01:48
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 xuwei-k/7c3f662a78fdd39ce00db43900470848 to your computer and use it in GitHub Desktop.
Save xuwei-k/7c3f662a78fdd39ce00db43900470848 to your computer and use it in GitHub Desktop.
sbt.version=1.4.7
scalaVersion := "3.0.0-RC1"
import scala.deriving.Mirror
import scala.compiletime.{erasedValue, summonInline}
object Macros {
inline def values[A](using inline A: Mirror.SumOf[A]): List[A] =
summonValues[A.MirroredElemTypes].asInstanceOf[List[A]]
inline def summonValues[T <: Tuple]: List[Any] =
inline erasedValue[T] match {
case _: EmptyTuple =>
Nil
case _: (t *: ts) =>
summonInline[ValueOf[t]].value :: summonValues[ts]
}
}
sealed trait A
case object B extends A
case object C extends A
case object D extends A
object Main {
def main(args: Array[String]): Unit = {
val x = Macros.values[A]
println(x)
assert(x.toSet == Set(B, C, D))
}
}
addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % "0.5.3")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment