Erased Definitions - More Details
Rules
-
erasedis a soft modifier. It can appear in avaldefinition or in a parameter.erasedcannot appear in alazyvaldefinition.erasedcan appear for a parameterless given that expands to a value definition. In that case thegivenis expanded to a non-lazyval.erasedcannot appear in a call-by-name parameter.erasedcannot appear in a mutablevardefinition.erasedcannot appear in anobjectdefinition.
-
Values or parameters that have a type that extends the
scala.compiletime.Erasedtrait 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
erasedvalue can only be used in an erased context:- Inside the expression of an argument to an
erasedparameter - Inside the body of an
erasedval - Inside the path of a dependent type expression
- Inside the expression of an argument to an
-
erasedcan 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) => RandT => R(or(erased T) ?=> RandT ?=> R). Theerasedparameters must match exactly in their respective positions. -
Eta expansion
if
def f(erased x: T): Uthenf: (erased T) => U. -
Erasure semantics
- All
erasedparameters are removed from the function - All arguments to
erasedparameters are not passed to the function - All
erasedvalue definitions are removed - All
erasedargument types are removed from a function type
- All
-
Overloading
Method with
erasedparameters will follow the normal overloading constraints after erasure. -
Overriding
- Member definitions overriding each other must both be
erasedor not beerased. def foo(x: T): Ucannot be overridden bydef foo(erased x: T): Uand 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