You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Change the condition when type parameters of an enum are added to a class case.
Quoting my comment on #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).
Copy file name to clipboardExpand all lines: docs/docs/reference/enums/desugarEnums.md
+18-7Lines changed: 18 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -25,9 +25,9 @@ some terminology and notational conventions:
25
25
26
26
The desugaring rules imply that class cases are mapped to case classes, and singleton cases are mapped to `val` definitions.
27
27
28
-
There are eight desugaring rules. Rule (1) desugar enum definitions. Rules
28
+
There are nine desugaring rules. Rule (1) desugar enum definitions. Rules
29
29
(2) and (3) desugar simple cases. Rules (4) to (6) define extends clauses for cases that
30
-
are missing them. Rules (7) and (8) define how such cases with extends clauses
30
+
are missing them. Rules (7) to (9) define how such cases with extends clauses
31
31
map into case classes or vals.
32
32
33
33
1. An `enum` definition
@@ -80,7 +80,7 @@ map into case classes or vals.
80
80
case C extends E[B1, ..., Bn]
81
81
82
82
where `Bi` is `Li` if `Vi = '+'` and `Ui` if `Vi = '-'`. This result is then further
83
-
rewritten with rule (7). Simple cases of enums with non-variant type
83
+
rewritten with rule (8). Simple cases of enums with non-variant type
84
84
parameters are not permitted.
85
85
86
86
5. A class case without an extends clause
@@ -91,7 +91,7 @@ map into case classes or vals.
91
91
92
92
case C <type-params> <value-params> extends E
93
93
94
-
This result is then further rewritten with rule (8).
94
+
This result is then further rewritten with rule (9).
95
95
96
96
6. If `E` is an enum with type parameters `Ts`, a class case with neither type parameters nor an extends clause
97
97
@@ -101,9 +101,20 @@ map into case classes or vals.
101
101
102
102
case C[Ts] <value-params> extends E[Ts]
103
103
104
-
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.
104
+
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.
105
105
106
-
7. A value case
106
+
7. If `E` is an enum with type parameters `Ts`, a class case without type parameters but with an extends clause
107
+
108
+
case C <value-params> extends <parents>
109
+
110
+
expands to
111
+
112
+
case C[Ts] <value-params> extends <parents>
113
+
114
+
provided at least one of the parameters `Ts` is mentioned in a parameter type in
115
+
`<value-params>` or in a type argument in `<parents>`.
116
+
117
+
8. A value case
107
118
108
119
case C extends <parents>
109
120
@@ -116,7 +127,7 @@ map into case classes or vals.
116
127
as one of the `enumValues` of the enumeration (see below). `$values` is a
117
128
compiler-defined private value in the companion object.
0 commit comments