Skip to content

Commit fa46ab2

Browse files
Update scala 3 macro to support a hierarchy of sealed traits
1 parent 74a7b36 commit fa46ab2

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

macros/src/main/scala-3/enumeratum/ValueEnumMacros.scala

+16-2
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ object ValueEnumMacros {
109109
tpe: Type[A],
110110
valueTpe: Type[ValueType]
111111
)(using cls: ClassTag[ValueType]): Expr[IndexedSeq[A]] = {
112-
type TakeHead[Head <: A & Singleton, Tail <: Tuple] = Head *: Tail
112+
type SingletonHead[Head <: A & Singleton, Tail <: Tuple] = Head *: Tail
113+
type ProductHead[Head <: A, Tail <: Tuple] = Head *: Tail
113114

114115
type SumOf[X <: A, T <: Tuple] = Mirror.SumOf[X] {
115116
type MirroredElemTypes = T
@@ -186,7 +187,7 @@ In SBT settings:
186187
values: Map[TypeRepr, ValueType]
187188
)(using tupleTpe: Type[T]): Either[String, Expr[List[A]]] =
188189
tupleTpe match {
189-
case '[TakeHead[h, tail]] => {
190+
case '[SingletonHead[h, tail]] => {
190191
val htpr = TypeRepr.of[h]
191192

192193
(for {
@@ -224,6 +225,19 @@ In SBT settings:
224225
}
225226
}
226227

228+
case '[ProductHead[h, tail]] =>
229+
Expr.summon[Mirror.SumOf[h]] match {
230+
case Some(sum) =>
231+
sum.asTerm.tpe.asType match {
232+
case '[SumOf[a, t]] => collect[Tuple.Concat[t, tail]](instances, values)
233+
234+
case _ => Left(s"Invalid `Mirror.SumOf[${TypeRepr.of[h].show}]")
235+
}
236+
237+
case None =>
238+
Left(s"Missing `Mirror.SumOf[${TypeRepr.of[h].show}]`")
239+
}
240+
227241
case '[EmptyTuple] => {
228242
val allowAlias = repr <:< TypeRepr.of[AllowAlias]
229243

0 commit comments

Comments
 (0)