Search code examples
scalascala-macrostypechecking

Compile-time Reflection of Nested List: typecheck List[List[Int]] returns List[List[...]]?


I'm using macro annotations to inspect the fields of a class and add a member based on those fields.

e.g.

@AddVal
class A(x: Int)

expands to

class A(x: Int){
  val get: Int = x
}

After extracting theValDef, it's tpe field still null so to get the type I have two options:

1) If I call .toString on the type tree, I can see the type, but now I've lost some type-safety

2) If I use c.typecheck on the type tree, I can get the type, but only if it's 1 level deep. List[List[Int]] comes back as List[List[...]]

        val fieldType = c.typecheck(q"type T = ${f.tpt}") match {
          case x @ TypeDef(mods, name, tparams, rhs)  => rhs.tpe
        }

So, is there a way to recursively typecheck polytypes?

I tried typechecking rhs again but I got The argument types of an anonymous function must be fully known and I'm not sure how to resolve that.

Thanks for taking a look,

Julian


Solution

  • I incorrectly attributed this error to the macro, when in fact there was another underlying macro (a type provider macro) that was failing to provide the proper nested type (in this case the Int).