Created
May 19, 2021 13:09
-
-
Save kubukoz/60117a3e00af108ae6933d153b7041cc 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.* | |
import scala.compiletime.* | |
inline def valuesOf[T <: Tuple]: List[String] = inline erasedValue[T] match { | |
case _: EmptyTuple => Nil | |
case _: (h *: t) => | |
erasedValue[h] match { | |
case label: String => | |
label :: valuesOf[t] | |
} | |
} | |
inline def derived[T](using s: Mirror.SumOf[T]): Labels[T] = | |
a => valuesOf[s.MirroredElemLabels].toList(s.ordinal(a)) |
Got it:
import scala.deriving.*
import scala.compiletime.*
inline def derived[T](using s: Mirror.SumOf[T]): Labels[T] =
a =>
val labels = constValueTuple[s.MirroredElemLabels].toList
val allStrings = summonInline[labels.type <:< List[String]]
allStrings(labels)(s.ordinal(a))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Better, but still with a cast: