I was wondering what’s the difference between Wildcard types and TypeBounds in the compiler.
The language specification seems to only allow Wildcards as arguments to ParameterizedType’s (I’m assuming these correspond to AppliedType’s in the implementation), and always require them to have TypeBounds. Whereas TypeBounds can be used to add more information to TypeParam’s or Refinement’s (and Wildcard’s), but not as a standalone type. Does this match how it is/should be used in the compiler?
The Tasty format mentions TypeBounds in a few places, but not Wildcard types. If we were to attempt to pickle a type with a Wildcard type it would throw a MatchError in TreePickler (I’m not sure if this can be triggered organically in the current compiler). Is the fact that Tasty doesn’t support Wildcard types intentional?
TypeBounds can be created by the user (e.g. Foo[? <: Bar]). Wildcard types can be created by the SpaceEngine when it indirectly calls ProtoTypes.wildApprox. Are there other ways the user or the compiler creates Wildcard types? When should one be used over the other?
The compiler’s TypeBounds acts both as the spec’s abstract type definition (TypeBounds) and wildcard type argument (WildcardTypeArg), depending on where it is used. TASTy has the same double usage.
What the compiler calls a WildcardType does not exist in the spec nor in TASTy. It is an internal thing used for type inference and elaboration only.
They shouldn’t be present in the type of any tree at any phase, just in “prototypes” used temporarily in the compiler. This might happen at different phases, for example they’re used during implicit resolution which can happen after Typer (in particular when using summonInline in a non-transparent inline def).
I see.
This question was originally prompted because I’m trying to better understand TypeOps.refineUsingParent, which will add WildcardTypes instead of TypeBounds to the type parameters it returns.
With the following source code:
sealed trait Foo[+A]
case class Bar[A](a: A) extends Foo[A]
case object Baz extends Foo[Nothing]
def unFoo[A](f: Foo[A]): Option[A] = f match {
case Bar(a) => Some(a)
case _ => None
}
Trace of running refineUsingParent( Foo[A], Bar ) returns a WildcardType (shown as <? <: A>):
As far as I can tell refineUsingParent is only used in the SpaceEngine right now, and the resulting type is never stored in a tree.
However, the question I have is whether it should return types containing WildcardTypes instead of TypeBounds, given that it’s not really used as a “prototype”?
When WildcardType is used in a prototype, what is the difference between a WildcardType that just wraps a TypeBounds and the underlying TypeBounds itself? Another way of phrasing the question: in a prototype, why does the compiler use a WildcardType instead of just the TypeBounds?