TypeErasure

object TypeErasure

Erased types are:

ErasedValueType TypeRef(prefix is ignored, denot is ClassDenotation) TermRef(prefix is ignored, denot is SymDenotation) JavaArrayType AnnotatedType MethodType ThisType SuperType ClassInfo (NoPrefix, ...) NoType NoPrefix WildcardType ErrorType

only for isInstanceOf, asInstanceOf: PolyType, TypeParamRef, TypeBounds

Companion
class
class Object
trait Matchable
class Any

Type members

Classlikes

final class CachedErasedValueType(tycon: TypeRef, erasedUnderlying: Type) extends ErasedValueType
abstract case class ErasedValueType(tycon: TypeRef, erasedUnderlying: Type) extends CachedGroundType with ValueType

A type representing the semi-erasure of a derived value class, see SIP-15 where it's called "C$unboxed" for a class C. Derived value classes are erased to this type during Erasure (when semiEraseVCs = true) and subsequently erased to their underlying type during ElimErasedValueType. This type is outside the normal Scala class hierarchy: it is a subtype of no other type and is a supertype only of Nothing. This is because this type is only useful for type adaptation (see Erasure.Boxing#adaptToType).

A type representing the semi-erasure of a derived value class, see SIP-15 where it's called "C$unboxed" for a class C. Derived value classes are erased to this type during Erasure (when semiEraseVCs = true) and subsequently erased to their underlying type during ElimErasedValueType. This type is outside the normal Scala class hierarchy: it is a subtype of no other type and is a supertype only of Nothing. This is because this type is only useful for type adaptation (see Erasure.Boxing#adaptToType).

Value Params
erasedUnderlying

The erased type of the single field of the value class

tycon

A TypeRef referring to the value class symbol

Companion
object
Companion
class

Value members

Concrete methods

def erasedGlb(tp1: Type, tp2: Type)(using Context): Type

The erased greatest lower bound of two erased type picks one of the two argument types.

The erased greatest lower bound of two erased type picks one of the two argument types.

This operation has the following the properties:

  • Associativity and commutativity, because this method acts as the minimum of the total order induced by compareErasedGlb.
  • Java compatibility: intersections that would be valid in Java code are erased like javac would erase them (a Java intersection is composed of exactly one class and one or more interfaces and always erases to the class).
def erasedGlb(tps: List[Type])(using Context): Type

Overload of erasedGlb to compare more than two types at once.

Overload of erasedGlb to compare more than two types at once.

def erasedLub(tp1: Type, tp2: Type)(using Context): Type

The erased least upper bound of two erased types is computed as follows

The erased least upper bound of two erased types is computed as follows

  • if both argument are arrays of objects, an array of the erased lub of the element types
  • if both arguments are arrays of same primitives, an array of this primitive
  • if one argument is array of primitives and the other is array of objects, Object
  • if one argument is an array, Object
  • otherwise a common superclass or trait S of the argument classes, with the following two properties: S is minimal: no other common superclass or trait derives from S S is last : in the linearization of the first argument type tp1 there are no minimal common superclasses or traits that come after S. The reason to pick last is that we prefer classes over traits that way, which leads to more predictable bytecode and (?) faster dynamic dispatch.
def erasedRef(tp: Type)(using Context): Type

The erasure of a top-level reference. Differs from normal erasure in that TermRefs are kept instead of being widened away.

The erasure of a top-level reference. Differs from normal erasure in that TermRefs are kept instead of being widened away.

def erasure(tp: Type)(using Context): Type

The standard erasure of a Scala type. Value classes are erased as normal classes.

The standard erasure of a Scala type. Value classes are erased as normal classes.

Value Params
tp

The type to erase.

def fullErasure(tp: Type)(using Context): Type

Like value class erasure, but value classes erase to their underlying type erasure

Like value class erasure, but value classes erase to their underlying type erasure

Does the (possibly generic) type tp have the same erasure in all its possible instantiations?

Does the (possibly generic) type tp have the same erasure in all its possible instantiations?

def isErasedType(tp: Type)(using Context): Boolean

A predicate that tests whether a type is a legal erased type. Only asInstanceOf and isInstanceOf may have types that do not satisfy the predicate. ErasedValueType is considered an erased type because it is valid after Erasure (it is eliminated by ElimErasedValueType).

A predicate that tests whether a type is a legal erased type. Only asInstanceOf and isInstanceOf may have types that do not satisfy the predicate. ErasedValueType is considered an erased type because it is valid after Erasure (it is eliminated by ElimErasedValueType).

def isGeneric(tp: Type)(using Context): Boolean

Is tp an abstract type or polymorphic type parameter, or another unbounded generic type?

Is tp an abstract type or polymorphic type parameter, or another unbounded generic type?

def isGenericArrayElement(tp: Type, isScala2: Boolean)(using Context): Boolean

Is Array[tp] a generic Array that needs to be erased to Object? This is true if among the subtypes of Array[tp] there is either:

Is Array[tp] a generic Array that needs to be erased to Object? This is true if among the subtypes of Array[tp] there is either:

  • both a reference array type and a primitive array type (e.g. Array[_ <: Int | String], Array[_ <: Any])
  • or two different primitive array types (e.g. Array[_ <: Int | Double]) In both cases the erased lub of those array types on the JVM is Object.

In addition, if isScala2 is true, we mimic the Scala 2 erasure rules and also return true for element types upper-bounded by a non-reference type such as in Array[_ <: Int] or Array[_ <: UniversalTrait].

The current context with a phase no later than erasure

The current context with a phase no later than erasure

def scala2Erasure(tp: Type)(using Context): Type

The erasure that Scala 2 would use for this type.

The erasure that Scala 2 would use for this type.

def sigName(tp: Type, sourceLanguage: SourceLanguage)(using Context): TypeName
def transformInfo(sym: Symbol, tp: Type)(using Context): Type

The symbol's erased info. This is the type's erasure, except for the following symbols:

The symbol's erased info. This is the type's erasure, except for the following symbols:

  • For $asInstanceOf : [T]T
  • For $isInstanceOf : [T]Boolean
  • For all abstract types : = ?

sourceLanguage, isConstructor and semiEraseVCs are set based on the symbol.

def valueErasure(tp: Type)(using Context): Type

The value class erasure of a Scala type, where value classes are semi-erased to ErasedValueType (they will be fully erased in ElimErasedValueType).

The value class erasure of a Scala type, where value classes are semi-erased to ErasedValueType (they will be fully erased in ElimErasedValueType).

Value Params
tp

The type to erase.