https://gist.github.com/xuwei-k/7c3f662a78fdd39ce00db43900470848
Created
February 21, 2021 02:01
-
-
Save xuwei-k/c798d8faa3f4f163be0aad150b327d07 to your computer and use it in GitHub Desktop.
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 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