Skip to content

Commit

Permalink
Change enum Desugarings
Browse files Browse the repository at this point in the history
Change the condition when type parameters of an enum are added to a class case.

Quoting my comment on scala#6095:

We have the following possibilities:

 1. type parameters are added only of there is no extends clause,
 2. (all) type parameters are added if one of them is referenced in the case,
 3. type parameters are always added if the case has value parameters (i.e. is not translated to an object).

(1) is what the current rules say. (2) is what Adriaan proposed. (3) is what is currently implemented.

In addition we have to clarify what should happen if a type parameter that is not added is nevertheless referred to in the case. Since enum expansion is done during desugaring, this could silently rebind to some other type. We should come up with a solution that avoids this, if possible.

This PR changes the rules to implement (2).
  • Loading branch information
odersky committed Mar 24, 2019
1 parent acb2ceb commit 4470c9e
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions docs/docs/reference/enums/desugarEnums.md
Expand Up @@ -25,9 +25,9 @@ some terminology and notational conventions:

The desugaring rules imply that class cases are mapped to case classes, and singleton cases are mapped to `val` definitions.

There are eight desugaring rules. Rule (1) desugar enum definitions. Rules
There are nine desugaring rules. Rule (1) desugar enum definitions. Rules
(2) and (3) desugar simple cases. Rules (4) to (6) define extends clauses for cases that
are missing them. Rules (7) and (8) define how such cases with extends clauses
are missing them. Rules (7) to (9) define how such cases with extends clauses
map into case classes or vals.

1. An `enum` definition
Expand Down Expand Up @@ -80,7 +80,7 @@ map into case classes or vals.
case C extends E[B1, ..., Bn]

where `Bi` is `Li` if `Vi = '+'` and `Ui` if `Vi = '-'`. This result is then further
rewritten with rule (7). Simple cases of enums with non-variant type
rewritten with rule (8). Simple cases of enums with non-variant type
parameters are not permitted.

5. A class case without an extends clause
Expand All @@ -91,7 +91,7 @@ map into case classes or vals.

case C <type-params> <value-params> extends E

This result is then further rewritten with rule (8).
This result is then further rewritten with rule (9).

6. If `E` is an enum with type parameters `Ts`, a class case with neither type parameters nor an extends clause

Expand All @@ -101,9 +101,20 @@ map into case classes or vals.

case C[Ts] <value-params> extends E[Ts]

This result is then further rewritten with rule (8). For class cases that have type parameters themselves, an extends clause needs to be given explicitly.
This result is then further rewritten with rule (9). For class cases that have type parameters themselves, an extends clause needs to be given explicitly.

7. A value case
7. If `E` is an enum with type parameters `Ts`, a class case without type parameters but with an extends clause

case C <value-params> extends <parents>

expands to

case C[Ts] <value-params> extends <parents>

provided at least one of the parameters `Ts` is mentioned in a parameter type in
`<value-params>` or in a type argument in `<parents>`.

8. A value case

case C extends <parents>

Expand All @@ -116,7 +127,7 @@ map into case classes or vals.
as one of the `enumValues` of the enumeration (see below). `$values` is a
compiler-defined private value in the companion object.

8. A class case
9. A class case

case C <params> extends <parents>

Expand Down

0 comments on commit 4470c9e

Please sign in to comment.