E218: Illegal Erased Definition
This error is emitted when the erased modifier is used with a definition that doesn't support it.
Only non-lazy immutable values (val) can be marked as erased. The erased modifier is not allowed for:
- Methods (except macros)
- Lazy values (
lazy val) - Mutable values (
var) - Type definitions
- Objects
Additionally, this error can occur when a value's type implicitly makes it erased (by extending compiletime.Erased) but the definition kind doesn't support erasure.
Example
import scala.language.experimental.erasedDefinitions
object Test:
erased lazy val x: Int = 42
Error
-- [E218] Type Error: example.scala:4:18 ---------------------------------------
4 | erased lazy val x: Int = 42
| ^
| `erased` is not allowed for this kind of definition.
|-----------------------------------------------------------------------------
| Explanation (enabled by `-explain`)
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| Only non-lazy immutable values can be `erased`
-----------------------------------------------------------------------------
Solution
Use erased only with non-lazy val definitions:
import scala.language.experimental.erasedDefinitions
object Test:
erased val x: Int = 42
If you need a method with erased parameters, use erased parameters instead:
import scala.language.experimental.erasedDefinitions
def compute(erased hint: Int): Int = 42
In this article