How to create a substitution pattern in a Scala macro?

I cannot learn how to programmatically build existential types in Scala macros.

For example, suppose I have ClassSymbolone that represents a class C[T]that has one type parameter.

Now, how do I programmatically create a type C[_ <: java.lang.Number]?

In particular, I do not know how to use the constructor object ExistentialType. Looking at his signature:

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

What am I conveying how quantified?

+4
source share
1 answer

, , , . , -Ymacro-debug-lite typeOf[C[_ <: Number]]. , , , , 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)))))
}
+1

All Articles