Boxing

object Boxing
class Object
trait Matchable
class Any

Value members

Concrete methods

def adaptClosure(tree: Closure)(using Context): Tree

The following code:

The following code:

val f: Function1[Int, Any] = x => ...

results in the creation of a closure and an implementation method in the typer:

def $anonfun(x: Int): Any = ...
val f: Function1[Int, Any] = closure($anonfun)

Notice that $anonfun takes a primitive as argument, but the SAM (Single Abstract Method) of Function1 after erasure is:

def apply(x: Object): Object

which takes a reference as argument. Hence, some form of adaptation is required. The most reliable way to do this adaptation is to replace the closure implementation method by a bridge method that forwards to the original method with appropriate boxing/unboxing. For our example above, this would be:

def $anonfun$adapted(x: Object): Object = $anonfun(BoxesRunTime.unboxToInt(x))
val f: Function1 = closure($anonfun$adapted)

But in some situations we can avoid generating this bridge, either because the runtime can perform auto-adaptation, or because we can replace the closure functional interface by a specialized sub-interface, see comments in this method for details.

See test cases lambda-*.scala and t8017/ for concrete examples.

def adaptToType(tree: Tree, pt: Type)(using Context): Tree

Adaptation of an expression e to an expected type PT, applying the following rewritings exhaustively as long as the type of e is not a subtype of PT.

Adaptation of an expression e to an expected type PT, applying the following rewritings exhaustively as long as the type of e is not a subtype of PT.

e -> e() if e appears not as the function part of an application e -> box(e) if e is of erased value type e -> unbox(e, PT) otherwise, if PT is an erased value type e -> box(e) if e is of primitive type and PT is not a primitive type e -> unbox(e, PT) if PT is a primitive type and e is not of primitive type e -> cast(e, PT) otherwise

final def box(tree: Tree, target: => String)(using Context): Tree
def boxMethod(cls: ClassSymbol)(using Context): Symbol
def cast(tree: Tree, pt: Type)(using Context): Tree

Generate a synthetic cast operation from tree.tpe to pt. Does not do any boxing/unboxing (this is handled upstream). Casts from and to ErasedValueType are special, see the explanation in ExtensionMethods#transform.

Generate a synthetic cast operation from tree.tpe to pt. Does not do any boxing/unboxing (this is handled upstream). Casts from and to ErasedValueType are special, see the explanation in ExtensionMethods#transform.

def constant(tree: Tree, const: Tree)(using Context): Tree
def etaExpand(tree: Tree, mt: MethodType, pt: Type)(using Context): Tree

Eta expand given tree that has the given method type mt, so that it conforms to erased result type pt. To do this correctly, we have to look at the tree's original pre-erasure type and figure out which context function types in its result are not yet instantiated.

Eta expand given tree that has the given method type mt, so that it conforms to erased result type pt. To do this correctly, we have to look at the tree's original pre-erasure type and figure out which context function types in its result are not yet instantiated.

def isBox(sym: Symbol)(using Context): Boolean
def isUnbox(sym: Symbol)(using Context): Boolean
def unbox(tree: Tree, pt: Type)(using Context): Tree