|
| 1 | +--- |
| 2 | +layout: blog-page |
| 3 | +title: Scala 3.0.0-M3: developer's preview before RC1 |
| 4 | +author: Anatolii Kmetiuk |
| 5 | +authorImg: /images/anatolii.png |
| 6 | +date: 2020-12-18 |
| 7 | +--- |
| 8 | +We are happy to announce the release of Scala 3.0.0-M3. This release is the Developer's Preview release. It is intended to contain all the features meant for RC1, which is tentatively planned for January 2021. The purpose of M3 is to give the larger community, beyond early adopters, a chance to try out all the features and give us feedback before sealing them in RC1. |
| 9 | + |
| 10 | +To collect the feedback, we have designed a [Scala 3 Developer’s Preview satisfaction survey (3-5 min)](https://docs.google.com/forms/d/e/1FAIpQLSflVmTu9lhrtnSTh2tKAjUGrt3WvEgwlDqZg66O3EVSXd1aJg/viewform?usp=sf_link) – please fill it in to express how you feel about Scala 3. |
| 11 | + |
| 12 | +During the weeks between M2 and M3, we have been tying up all the loose ends, so that everything intended for the release could be finished before ending 2020. In addition, we have used this time to review and fine-tune the language syntax. Finally, a large number of consultations with the community took place during this time. |
| 13 | + |
| 14 | +In this article, you will find the most important changes of this release compared to Scala 3.0.0-M2. |
| 15 | + |
| 16 | +To read more about all things that surround the Scala 3 release in the next months, check out the "[Scala 3 - Crossing the finish line](https://www.scala-lang.org/blog/2020/12/15/scala-3-crossing-the-finish-line.html)" blog post on scala-lang.org |
| 17 | + |
| 18 | +You can try out the M3 version online via [Scastie](https://scastie.scala-lang.org/?target=dotty). |
| 19 | + |
| 20 | +<!--more--> |
| 21 | +# sbt plugin update |
| 22 | +We published a new version of the sbt plugin `sbt-dotty`, v0.5.0. Because of the changes in PR [#10607](https://github.com/lampepfl/dotty/pull/10607), this release of Scala 3 will not work with earlier versions of sbt-dotty. You will need to upgrade sbt-dotty to 0.5.0 to be able to use Scala 3.0.0-M3. |
| 23 | + |
| 24 | +# Final syntactic tweaks |
| 25 | +## `as` dropped from the `given` syntax |
| 26 | +The following syntax is obsolete: |
| 27 | + |
| 28 | +```scala |
| 29 | +given intOrd as Ordering[Int]: |
| 30 | + ... |
| 31 | +given listOrd[T: Ordering] as Ordering[List[T]]: |
| 32 | + ... |
| 33 | + |
| 34 | +given Ordering[Int]: |
| 35 | + ... |
| 36 | +given [T: Ordering] as Ordering[List[T]]: |
| 37 | + ... |
| 38 | + |
| 39 | +given global as ExecutionContext = ForkJoinContext() |
| 40 | +given Context = ctx |
| 41 | +``` |
| 42 | + |
| 43 | +Here is how the above is expressed now: |
| 44 | + |
| 45 | +```scala |
| 46 | +given intOrd: Ordering[Int] with |
| 47 | + ... |
| 48 | +given listOrd[T: Ordering]: Ordering[List[T]] with |
| 49 | + ... |
| 50 | + |
| 51 | +given Ordering[Int] with |
| 52 | + ... |
| 53 | +given [T: Ordering]: Ordering[List[T]] with |
| 54 | + ... |
| 55 | + |
| 56 | +given global: ExecutionContext = ForkJoinContext() |
| 57 | +given Context = ctx |
| 58 | +``` |
| 59 | + |
| 60 | +You can find a discussion of the above change in the [PR #10538](https://github.com/lampepfl/dotty/pull/10538). |
| 61 | + |
| 62 | +## Drop `as` in patterns |
| 63 | +Since we dropped `as` from `given`s, we lost a strong reason for having `as` at all. Therefore, we dropped `as` from patterns as well. The following syntax, valid in Scala 3.0.0-M2, is not accepted anymore: |
| 64 | + |
| 65 | +```scala |
| 66 | +case opt as Some(foo) |
| 67 | +``` |
| 68 | + |
| 69 | +Instead, code that used it should be reverted to use the existing syntax of Scala 2, using `@`: |
| 70 | + |
| 71 | +```scala |
| 72 | +case opt @ Some(foo) |
| 73 | +``` |
| 74 | + |
| 75 | +## Switch back to the old context function closure syntax |
| 76 | +Previously, context function closures were written as follows: |
| 77 | + |
| 78 | +```scala |
| 79 | +(using s: Show[String]) => s.show("foobar") |
| 80 | +``` |
| 81 | + |
| 82 | +Now, we have reverted to the following syntax: |
| 83 | +```scala |
| 84 | +(s: Show[String]) ?=> s.show("foobar") |
| 85 | +``` |
| 86 | + |
| 87 | +# `Matchable` trait – a new top-level type |
| 88 | +Although opaque types are supposed to provide an opaque abstraction of their underlying types, it is currently too easy to break the abstraction using pattern matching: |
| 89 | + |
| 90 | +```scala |
| 91 | +val imm = IArray(1,2,3) // supposedly immutable... |
| 92 | +imm match |
| 93 | + case a: Array[Int] => a(0) = 0 // but that's shown to be lie |
| 94 | +``` |
| 95 | + |
| 96 | +To address this change, we introduce a new trait `Matchable` near the top of the type hierarchy: |
| 97 | + |
| 98 | +```scala |
| 99 | +abstract class Any: |
| 100 | + def asInstanceOf |
| 101 | + def == |
| 102 | + def != |
| 103 | + def ## |
| 104 | + def equals |
| 105 | + def hashCode |
| 106 | + def toString |
| 107 | + |
| 108 | +trait Matchable extends Any: |
| 109 | + def isInstanceOf |
| 110 | + def getClass |
| 111 | + |
| 112 | +class AnyVal extends Any, Matchable |
| 113 | + |
| 114 | +class Object extends Any, Matchable |
| 115 | +``` |
| 116 | + |
| 117 | +`Matchable` is currently a marker trait without any methods. Over time, we intend to migrate the methods `getClass` and `isInstanceOf` to it, since these are closely related to pattern-matching. |
| 118 | + |
| 119 | +In the meantime, the compiler will emit warnings when trying to call those methods, or when performing a `match`, on an value that is not a `Matchable`. For example, the above example is now flagged: |
| 120 | + |
| 121 | +```scala |
| 122 | +-- Warning: i7314.scala:6:12 --------------------------------------------------- |
| 123 | +6 | case a: Array[Int] => |
| 124 | + | ^^^^^^^^^^ |
| 125 | + | pattern selector should be an instance of Matchable, |
| 126 | + | but it has unmatchable type opaques.IArray[Int] instead |
| 127 | +``` |
| 128 | + |
| 129 | +Note that the warnings are only active with language mode `3.1-migration` or higher - see the documentation on the [Language Versions](https://dotty.epfl.ch/docs/usage/language-versions.html) to learn how to enable it. |
| 130 | + |
| 131 | +You can read the discussion of this change in the [PR #10670](https://github.com/lampepfl/dotty/pull/10670). You can also read more about it in the [documentation](https://dotty.epfl.ch/docs/reference/other-new-features/matchable.html). |
| 132 | + |
| 133 | +# Tooling improvements |
| 134 | +As we are getting closer to a stable release of Scala 3, the focus increasingly shifts on the tooling available to get started with Scala 3. |
| 135 | + |
| 136 | +For a while now, we are not using the old dottydoc documentation tool for building the documentation. We are developing an entirely new tool, scala3doc, from scratch. This new documentation tool is more robust and faster than the old one. |
| 137 | + |
| 138 | +As part of the tooling effort, this new Scala 3 documentation tool is rapidly improved. [PR #10522](https://github.com/lampepfl/dotty/pull/10522) proves that the doctool can generate documentation for the community build projects. You can access this documentation via the following [link](https://scala3doc.virtuslab.com/pr-master-docs/index.html). |
| 139 | + |
| 140 | +[PR #10491](https://github.com/lampepfl/dotty/pull/10491) introduced scripting support in Scala 3. Consider the following source named `Main.scala`: |
| 141 | + |
| 142 | +```scala |
| 143 | +@main def Test(name: String): Unit = |
| 144 | + println(s"Hello ${name}!") |
| 145 | +``` |
| 146 | + |
| 147 | +If you have Scala 3 binaries on your path (which you can get by following the steps on the [Dotty website](https://dotty.epfl.ch/), in the section "Try Dotty"), you can run the following command: |
| 148 | + |
| 149 | + $ scala Main.scala World |
| 150 | + |
| 151 | +This will compile the source in question to a temporary directory and run the discovered main method with the argument `World`. |
| 152 | + |
| 153 | +Note the difference from the Scala 2 scripting implementation. In Scala 2, we do not require the users to have a `main` method in their scripts due to it being too cumbersome to write. In Scala 3, thanks to the top-level definitions and the `@main` annotations, `main` methods are one-liners and hence are more suited for scripts. |
| 154 | + |
| 155 | +The documentation for this feature is available [here](https://dotty.epfl.ch/docs/usage/getting-started.html#scala-3-for-scripting). |
| 156 | + |
| 157 | +# Metaprogramming changes |
| 158 | +We have been polishing the metaprogramming API and making it more uniform. The following notable changes occurred between M2 and M3: |
| 159 | + |
| 160 | +- Add `Expr.asTerm` [#10694](https://github.com/lampepfl/dotty/pull/10694) |
| 161 | +- Add reflect `MatchCase` `TypeRepr` [#10735](https://github.com/lampepfl/dotty/pull/10735) |
| 162 | +- Rework reflect Symbol fields API [#10705](https://github.com/lampepfl/dotty/pull/10705) |
| 163 | +- Remove `Expr.StringContext.unapply` [#10675](https://github.com/lampepfl/dotty/pull/10675) |
| 164 | +- Rename `Liftable` to `ToExpr` and `Unliftable` to `FromExpr` [#10618](https://github.com/lampepfl/dotty/pull/10618) |
| 165 | +- Remove Unliftable[Unit] [#10570](https://github.com/lampepfl/dotty/pull/10570) |
| 166 | +- Remove reflect.LambdaType [#10548](https://github.com/lampepfl/dotty/pull/10548) |
| 167 | +- Add `scala.quoted.Expr.unapply` as dual of `Expr.apply` [#10580](https://github.com/lampepfl/dotty/pull/10580) |
| 168 | +- Move `Quotes` as last parameter in `ExprMap.transform` [#10519](https://github.com/lampepfl/dotty/pull/10519) |
| 169 | +- Rework reflect Constant API [#10753](https://github.com/lampepfl/dotty/pull/10753) |
| 170 | +- Unify quoted.report and reflect.Reporting [#10474](https://github.com/lampepfl/dotty/pull/10474) |
| 171 | +- Fix #10359: Add GivenSelector to reflection API [#10469](https://github.com/lampepfl/dotty/pull/10469) |
| 172 | +- Rework reflect show API [#10661](https://github.com/lampepfl/dotty/pull/10661) |
| 173 | +- Fix #10709: Add missing level check before inlining [#10781](https://github.com/lampepfl/dotty/pull/10781) |
| 174 | + |
| 175 | +# Let us know what you think! |
| 176 | +If you have questions or any sort of feedback, feel free to send us a message on our |
| 177 | +[Gitter channel](https://gitter.im/lampepfl/dotty). If you encounter a bug, please |
| 178 | +[open an issue on GitHub](https://github.com/lampepfl/dotty/issues/new). |
| 179 | + |
| 180 | + |
| 181 | +## Contributors |
| 182 | +Thank you to all the contributors who made this release possible 🎉 |
| 183 | + |
| 184 | +According to `git shortlog -sn --no-merges 3.0.0-M2..3.0.0-M3` these are: |
| 185 | + |
| 186 | +``` |
| 187 | + 80 Nicolas Stucki |
| 188 | + 73 Martin Odersky |
| 189 | + 64 Krzysztof Romanowski |
| 190 | + 32 Liu Fengyun |
| 191 | + 28 Aleksander Boruch-Gruszecki |
| 192 | + 22 Anatolii Kmetiuk |
| 193 | + 17 Guillaume Martres |
| 194 | + 17 Sébastien Doeraene |
| 195 | + 14 Andrzej Ratajczak |
| 196 | + 13 Tom Grigg |
| 197 | + 8 Filip Zybała |
| 198 | + 7 Lan, Jian |
| 199 | + 5 Olivier Blanvillain |
| 200 | + 5 Som Snytt |
| 201 | + 5 Jamie Thompson |
| 202 | + 4 Stéphane Micheloud |
| 203 | + 4 Lionel Parreaux |
| 204 | + 3 Adrien Piquerez |
| 205 | + 3 Artur Opala |
| 206 | + 3 Hanns Holger Rutz |
| 207 | + 3 Michael Pilquist |
| 208 | + 3 Michał Pałka |
| 209 | + 3 bishabosha |
| 210 | + 2 Jonathan Brachthäuser |
| 211 | + 2 Camila Andrea Gonzalez Williamson |
| 212 | + 1 Mikael Blomstrand |
| 213 | + 1 Francois GORET |
| 214 | + 1 Felix Mulder |
| 215 | + 1 Raphael Jolly |
| 216 | + 1 Robert Stoll |
| 217 | + 1 Ruslan Shevchenko |
| 218 | + 1 Seth Tisue |
| 219 | + 1 Eugene Yokota |
| 220 | + 1 Amadou CISSE |
| 221 | + 1 Akhtiam Sakaev |
| 222 | + 1 Martin Duhem |
| 223 | + 1 Tomasz Godzik |
| 224 | + 1 Matthew Pickering |
| 225 | + 1 odersky |
| 226 | +``` |
| 227 | + |
| 228 | +If you want to get your hands dirty and contribute to Scala 3, head to our [Getting Started page for new contributors](https://dotty.epfl.ch/docs/contributing/getting-started.html) and read the docs on contributing. You are also very welcome to contact someone from our team to help you get started and see what currently we need help with. |
| 229 | + |
| 230 | +We are looking forward to having you join the team of contributors. |
| 231 | + |
| 232 | +## Library authors: Join our community build |
| 233 | + |
| 234 | +Scala 3 is regularly tested against a sample of libraries known as the "community build". You can add your library to the [community build](https://github.com/lampepfl/dotty/tree/master/community-build) by submitting a PR. |
| 235 | + |
| 236 | +[Scastie]: https://scastie.scala-lang.org/?target=dotty |
| 237 | + |
| 238 | +[@odersky]: https://github.com/odersky |
| 239 | +[@DarkDimius]: https://github.com/DarkDimius |
| 240 | +[@smarter]: https://github.com/smarter |
| 241 | +[@felixmulder]: https://github.com/felixmulder |
| 242 | +[@nicolasstucki]: https://github.com/nicolasstucki |
| 243 | +[@liufengyun]: https://github.com/liufengyun |
| 244 | +[@OlivierBlanvillain]: https://github.com/OlivierBlanvillain |
| 245 | +[@biboudis]: https://github.com/biboudis |
| 246 | +[@allanrenucci]: https://github.com/allanrenucci |
| 247 | +[@Blaisorblade]: https://github.com/Blaisorblade |
| 248 | +[@Duhemm]: https://github.com/Duhemm |
| 249 | +[@AleksanderBG]: https://github.com/AleksanderBG |
| 250 | +[@milessabin]: https://github.com/milessabin |
| 251 | +[@anatoliykmetyuk]: https://github.com/anatoliykmetyuk |
0 commit comments