Skip to content

Instantly share code, notes, and snippets.

@xuwei-k
Created February 21, 2021 02:01
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/c798d8faa3f4f163be0aad150b327d07 to your computer and use it in GitHub Desktop.
Save xuwei-k/c798d8faa3f4f163be0aad150b327d07 to your computer and use it in GitHub Desktop.
import scala.deriving.Mirror
import scala.compiletime.{erasedValue, summonInline}
object Macros {
inline def values[A](using inline A: Mirror.SumOf[A]): Values[A.MirroredElemTypes] =
summonValues[A.MirroredElemTypes]
type Values[T <: Tuple] <: Tuple = T match {
case EmptyTuple =>
EmptyTuple
case t *: ts =>
t *: Values[ts]
}
inline def summonValues[T <: Tuple]: Values[T] =
inline erasedValue[T] match {
case _: EmptyTuple =>
EmptyTuple
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 typed[A](a: A): Unit = ()
def main(args: Array[String]): Unit = {
val x = Macros.values[A]
println(x)
typed[(B.type, C.type, D.type)](x)
assert(x.getClass == classOf[Tuple3[_, _, _]])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment