Skip to content

Commit 1223ea2

Browse files
committed
Explain erased classes in doc page
1 parent 8c42294 commit 1223ea2

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

docs/docs/reference/metaprogramming/erased-terms-spec.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ title: "Erased Terms Spec"
55

66
## Rules
77

8-
1. The `erased` modifier can appear:
8+
1. `erased` is a soft modifier. It can appear:
99
* At the start of a parameter block of a method, function or class
1010
* In a method definition
1111
* In a `val` definition (but not `lazy val` or `var`)
12+
* In a `class` or `trait` definition
1213

1314
```scala
1415
erased val x = ...
@@ -20,10 +21,11 @@ title: "Erased Terms Spec"
2021
def h(x: (erased Int) => Int) = ...
2122

2223
class K(erased x: Int) { ... }
24+
erased class E {}
2325
```
2426

2527

26-
2. A reference to an `erased` definition can only be used
28+
2. A reference to an `erased` val or def can only be used
2729
* Inside the expression of argument to an `erased` parameter
2830
* Inside the body of an `erased` `val` or `def`
2931

docs/docs/reference/metaprogramming/erased-terms.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
---
22
layout: doc-page
3-
title: "Erased Terms"
3+
title: "Erased Terms And Classes"
44
---
55

6+
`erased` is a modifier that expresses that some definition or expression is erased by the compiler instead of being represented in the compiled output. It is not yet part of the Scala language standard. To enable `erased`, turn on the language feature
7+
`experimental.erased`. This can be done with a language import
8+
```scala
9+
import scala.language.experimental.erased
10+
```
11+
or by setting the command line option `-language:experimental.erased`.
612
## Why erased terms?
713

814
Let's describe the motivation behind erased terms with an example. In the
@@ -187,4 +193,36 @@ end Machine
187193
m.turnOn().turnOn() // error: Turning on an already turned on machine
188194
```
189195

196+
## Erased Classes
197+
198+
`erased` can also be used as a modifier for a class. An erased class is intended to be used only in erased definitions. If the type of a val definition or parameter is
199+
a (possibly aliased, refined, or instantiated) erased class, the definition is assumed to be `erased` itself. Likewise, a method with an erased class return type is assumed to be `erased` itself. Since given instances expand to vals and defs, they are also assumed to be erased if the type they produce is an erased class. Finally
200+
function types with erased classes as arguments turn into erased function types.
201+
202+
Example:
203+
```scala
204+
erased class CanRead
205+
206+
val x: CanRead = ... // `x` is turned into an erased val
207+
val y: CanRead => Int = ... // the function is turned into an erased function
208+
def f(x: CanRead) = ... // `f` takes an erased parameter
209+
def g(): CanRead = ... // `g` is turned into an erased def
210+
given CanRead = ... // the anonymous given is assumed to be erased
211+
```
212+
The code above expands to
213+
```scala
214+
erased class CanRead
215+
216+
erased val x: CanRead = ...
217+
val y: (erased CanRead) => Int = ...
218+
def f(erased x: CanRead) = ...
219+
erased def g(): CanRead = ...
220+
erased given CanRead = ...
221+
```
222+
After erasure, it is checked that no references to values of erased classes remain and that no instances of erased classes are created. So the following would be an error:
223+
```scala
224+
val err: Any = CanRead() // error: illegal reference to erased class CanRead
225+
```
226+
Here, the type of `err` is `Any`, so `err` is not considered erased. Yet its initializing value is a reference to the erased class `CanRead`.
227+
190228
[More Details](./erased-terms-spec.md)

0 commit comments

Comments
 (0)