Expand case class default values example
Hi,
I think this defaultParamsInference is a great example I'd like to extend further to include the type of each case class field.
What amazes me is I didn't realize you can cast a Tree as you do here:
https://github.com/lampepfl/dotty-macro-examples/blob/main/defaultParamsInference/src/macro.scala#L18
I thought I would need to use the Symbol's signature to get the type but I don't see a lot of documentation for signature or how to access type information (such as individually selecting types from higher orders, such as List[Int], as enabled in scala 2 by typeSignature or scala.reflect.macros.Context.typeCheck).
So can someone please clarify how tree or signature can be used to get detailed type information for each case class field?
Thanks!
it seems getting type information is indeed not easy, as discussed here: https://github.com/lampepfl/dotty/issues/13553
They've since added https://github.com/lampepfl/dotty/pull/14124 but it is labeled experimental so not yet usable in stable builds.
Got it and there's a few ways to skin this cat, but this one I like the most from Gael since it accounts for higher kinds: https://stackoverflow.com/a/69073868/1080804
example:
// for a cc field of type Map[String, List[Int]], this gives you:
// Map, String, and List TypeReprs
// recursion may be necessary to get List[Int]: https://stackoverflow.com/a/69073868/1080804
val typeParams: List[TypeRepr] = ccField.owner.typeRef.memberType(ccField) match {
// from: https://docs.scala-lang.org/scala3/guides/contribution/arch-types.html
case AppliedType(thisType, args) => List(thisType) ++ args
case _ => Nil
}
val simpleStrings = typeParams.map(_.typeSymbol.fullName)