Iterating over subclasses of a sealed trait #15157
Unanswered
adamw
asked this question in
Metaprogramming
Replies: 1 comment 11 replies
-
I think this is a bug in import quoted.*
object TestMacro:
inline def test[T]: List[String] = ${ testImpl[T] }
def testImpl[T: Type](using Quotes): Expr[List[String]] =
import quotes.reflect.*
val tpe = TypeRepr.of[T]
val children = tpe.typeSymbol.children.map { childSymbol =>
val childTpe = tpe.memberType(childSymbol)
"Child TypeRepr: " + childTpe
// childTpe.asType match
// case '[c] =>
// println("Got Type!")
}
Expr(children) resulting in:
|
Beta Was this translation helpful? Give feedback.
11 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm trying to summon a typeclass instance for each child class of a sealed trait. So far I've got the following code (here simplified):
The
childTpe: TypeRepr
is correct, however I get an exception when converting it to aType
and trying to get a handle to the type parameterc
(so that later I can doExpr.summon[MyTypeClass[c]]
). Using the following invocation:The output is:
Is there a better (and working :) ) way to achieve the above goal?
Beta Was this translation helpful? Give feedback.
All reactions