Erased Definitions - More Details
Rules
-
erased
is a soft modifier. It can appear in aval
definition or in a parameter.erased
cannot appear in alazy
val
definition.erased
can appear for a parameterless given that expands to a value definition. In that case thegiven
is expanded to a non-lazyval
.erased
cannot appear in a call-by-name parameter.erased
cannot appear in a mutablevar
definition.erased
cannot appear in anobject
definition.
-
Values or parameters that have a type that extends the
scala.compiletime.Erased
trait are implicitlyerased
.- The restrictions of point (1) apply.
- Parameterless givens are treated like values.
- Mutable variables cannot have a time that extends
scala.compiletime.Erased
.
-
A reference to an
erased
value can only be used in an erased context:- Inside the expression of an argument to an
erased
parameter - Inside the body of an
erased
val
- Inside the path of a dependent type expression
- Inside the expression of an argument to an
-
erased
can also be used in a function type, e.g.(erased T1, T2) => R
(x: T1, y: erased T2) ?=> T
Note that there is no subtype relation between
(erased T) => R
andT => R
(or(erased T) ?=> R
andT ?=> R
). Theerased
parameters must match exactly in their respective positions. -
Eta expansion
if
def f(erased x: T): U
thenf: (erased T) => U
. -
Erasure semantics
- All
erased
parameters are removed from the function - All arguments to
erased
parameters are not passed to the function - All
erased
value definitions are removed - All
erased
argument types are removed from a function type
- All
-
Overloading
Method with
erased
parameters will follow the normal overloading constraints after erasure. -
Overriding
- Member definitions overriding each other must both be
erased
or not beerased
. def foo(x: T): U
cannot be overridden bydef foo(erased x: T): U
and vice-versa.
- Member definitions overriding each other must both be
-
Type Restrictions
- Polymorphic function literals with erased parameters are currently not supported, and will be rejected by the compiler. This is purely an implementation restriction, and might be lifted in the future.
In this article