Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand case class default values example #32

Open
evbo opened this issue Jul 22, 2022 · 2 comments
Open

Expand case class default values example #32

evbo opened this issue Jul 22, 2022 · 2 comments

Comments

@evbo
Copy link

evbo commented Jul 22, 2022

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!

@evbo
Copy link
Author

evbo commented Jul 22, 2022

it seems getting type information is indeed not easy, as discussed here:
scala/scala3#13553

They've since added scala/scala3#14124 but it is labeled experimental so not yet usable in stable builds.

@evbo
Copy link
Author

evbo commented Jul 22, 2022

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant