How to construct wildcard type in Scala macro?

234 views Asked by At

I am unable to find out how to programatically construct existential types in Scala macros.

For example, let's assume that I have a ClassSymbol that represents a class C[T] that has one type parameter.

Now, how do I programatically construct the type C[_ <: java.lang.Number] ?

In particular, I have no idea how to use the ExistentialType constructor object. Looking at its signature:

def apply(quantified: List[Symbol], underlying: Type): ExistentialType

What do I pass as quantified?

1

There are 1 answers

3
Eugene Burmako On BEST ANSWER

From what I understand, you need to create quantified symbols yourself, setting their signatures manually. Here's an abridged and adapted version of what -Ymacro-debug-lite printed when I compiled typeOf[C[_ <: Number]]. Maybe Jason or Paul know a shortcut that avoids creating symbols manually, but I'm not sure the one exists in the public API.

class C[T]

object Test extends App {
  import scala.reflect.runtime.universe._
  val c = typeOf[C[_]].typeSymbol
  val targ = build.newNestedSymbol(NoSymbol, newTypeName("_$1"), NoPosition, build.flagsFromBits(34359738384L), false)
  build.setTypeSignature(targ, TypeBounds(typeOf[Nothing], typeOf[Number]))
  println(ExistentialType(List(targ), TypeRef(c.owner.asClass.thisPrefix, c, List(TypeRef(NoPrefix, targ, Nil)))))
}