Scala 3 macro: equivalent of knownDirectSubclasses

Hi,

I’m trying to port a project using macros to visit sum and product types to scala 3: GitHub - sangria-graphql/macro-visit: A macro-based generic visitor generator

This library is using knownDirectSubclasses to fetch the sum types of a sealed trait:

I’m trying to do the same in scala 3, but could not find how.
The Mirror.Of seems to work only if we want to derive a typeclass AFAIU. In this library, we go through the sum and product types without type classes.

Thanks in advance for any help.

1 Like

I’ve got a start:

  private def collectKnownSubtypes[T](using quotes: Quotes, tpe: Type[T]): Set[quotes.reflect.Symbol] = {
    import quotes.reflect._
    val children: List[Symbol] = TypeTree.of[T].symbol.children
    children.flatMap { s =>
      val flags = s.flags
      if (flags.is(Flags.Sealed) && flags.is(Flags.Trait)) {
        println("sealed trait " + s)
        // TODO: fetch children of sealed trait
        // println(boxTpe.memberType(s))
        // val childTpe: TypeTree = TypeIdent(s)
        // val childType: TypeRepr = childTpe.tpe
        // val tt = Type.of(childType)
        // collectKnownSubtypes[tt.Underlying](quotes, childType)
        s :: Nil
      } else {
        s :: Nil
      }
    }
    children.toSet
  }

But I still need to recursively extract children of any intermediate sealed traits.