Skip to content

Commit 6c19236

Browse files
committed
Update docs
1 parent 657caec commit 6c19236

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

docs/docs/reference/enums/adts.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,15 @@ Note that the type of the expressions above is always `Option`. That
5252
is, the implementation case classes are not visible in the result
5353
types of their `apply` methods. This is a subtle difference with
5454
respect to normal case classes. The classes making up the cases do
55-
exist, and can be unveiled by constructing them directly with a `new`.
55+
exist, and can be unveiled, either by constructing them directly with a `new`,
56+
or by explicitly providing an expected type.
57+
5658

5759
```scala
5860
scala> new Option.Some(2)
59-
val res3: t2.Option.Some[Int] = Some(2)
61+
val res3: Option.Some[Int] = Some(2)
62+
scala> val x: Option.Some[Int] = Option.Some(3)
63+
val res4: Option.Some[Int] = Some(3)
6064
```
6165

6266
As all other enums, ADTs can define methods. For instance, here is `Option` again, with an

docs/docs/reference/enums/desugarEnums.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,7 @@ map into `case class`es or `val`s.
139139
```scala
140140
final case class C <params> extends <parents>
141141
```
142-
However, unlike for a regular case class, the return type of the associated
143-
`apply` method is a fully parameterized type instance of the enum class `E`
144-
itself instead of `C`. Also the enum case defines an `ordinal` method of
145-
the form
142+
The enum case defines an `ordinal` method of the form
146143
```scala
147144
def ordinal = n
148145
```
@@ -153,6 +150,14 @@ map into `case class`es or `val`s.
153150
in a parameter type in `<params>` or in a type argument of `<parents>`, unless that parameter is already
154151
a type parameter of the case, i.e. the parameter name is defined in `<params>`.
155152

153+
The compiler-generated `apply` and `copy` methods of an enum case
154+
```scala
155+
case C(ps) extends P1, ..., Pn
156+
```
157+
are treated specially. A call `C(ts)` of the apply method is ascribed the underlying type
158+
`P1 & ... & Pn` (dropping any [super traits](../other-new-features/super-traits.html))
159+
as long as that type is still compatible with the expected type at the point of application.
160+
A call `t.copy(ts)` of `C`'s `copy` method is treated in the same way.
156161

157162
### Translation of Enums with Singleton Cases
158163

0 commit comments

Comments
 (0)