Skip to content

Commit 5ba5fc9

Browse files
bishaboshaanatoliykmetyuk
authored andcommitted
add section about enums
1 parent af24df3 commit 5ba5fc9

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

docs/blog/_posts/2020-11-09-scala3-m1.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ November 2020 brings an important milestone for Scala 3 – the release of Scala
99

1010
Once 3.0.0 release candidate is out, no new features or breaking changes will take place on 3.0.x – it will only be updated for bug fixes. However, we are going to continue the work on making the language better and to test out our research in it. These changes will end up in Scala only as of 3.1.0.
1111

12-
For now though, our teams are focusing the efforts on getting done with the remainder of the 40-something projects that we planned for the upcoming release back in July 2020. Many of them are already completed. Those that aren't yet – get revised and re-planned. With the current global uncertainty and with the scale of the project, cannot be certain, but we believe we have a reasonable chance of releasing 3.0.0-RC1 by Christmas.
12+
For now though, our teams are focusing the efforts on getting done with the remainder of the 40-something projects that we planned for the upcoming release back in July 2020. Many of them are already completed. Those that aren't yet – get revised and re-planned. With the current global uncertainty and with the scale of the project, we cannot be certain, but we believe we have a reasonable chance of releasing 3.0.0-RC1 by Christmas.
1313

1414
Below, you can find a short summary of the changes that took place during between the 0.27.0-RC1 and 3.0.0-M1 releases.
1515

@@ -80,6 +80,49 @@ import p.given
8080

8181
This change was implemented by PR [#9949](https://github.com/lampepfl/dotty/pull/9949).
8282

83+
# Final API for enumerations
84+
`enum` definitions are now released in their final design. since `0.27.0-RC1` we have made the following changes:
85+
86+
For the enum definition of Option:
87+
```scala
88+
enum Opt[+T] {
89+
case Sm(value: T)
90+
case Nn
91+
}
92+
```
93+
we will now generate on the companion objects of class cases `apply` and `copy` methods with the precise subtype:
94+
```scala
95+
object Sm {
96+
...
97+
def apply[T](value: T): Sm[T]
98+
...
99+
}
100+
```
101+
however expressions that call `apply` or `copy` will be widened to the parent enum type unless the precise type is expected, as we see here:
102+
```scala
103+
scala> Sm(23)
104+
val res0: Opt[Int] = Sm(23)
105+
106+
scala> val sm: Sm[23] = Sm(23)
107+
val sm: Opt.Sm[23] = Sm(23)
108+
```
109+
Previously, when an enumeration declared cases with value parameters, such as `Opt.Sm`, then the `Opt.values` array would no longer have indexes that match the enum case ordinals. We feel that this is problematic, so the `values` and `valueOf` methods will only be generated when an enum has exclusively singleton cases.
110+
111+
If an enumeration adds a case with value parameters, then consumers will recieve an error that explains why `values` has been removed.
112+
```scala
113+
scala> Opt.values
114+
1 |Opt.values
115+
|^^^^^^^^^^
116+
|value values is not a member of object Opt.
117+
|Although class Opt is an enum, it has non-singleton cases,
118+
|meaning a values array is not defined
119+
```
120+
For code that previously relied upon `values` to lookup singleton cases, we now provide an optimised method `fromOrdinal` that reflects singleton values. This method is always generated:
121+
```scala
122+
scala> Opt.fromOrdinal(1)
123+
val res1: Opt[?] = Nn
124+
```
125+
83126
# Keep `@alpha` optional for operators
84127
Following the discussion on [contributors](https://contributors.scala-lang.org/t/the-alpha-notation/4583), we now keep `@alpha` optional for operators. The checking behavior is still available when compiling with the `-Yrequire-alpha`.
85128

0 commit comments

Comments
 (0)