Skip to content

Commit a05216a

Browse files
Merge pull request #1591 from Zelenya/migration-to-scala-3
Add an article about migration to Scala 3
2 parents cfdeb47 + 07d0c35 commit a05216a

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
---
2+
layout: blog-detail
3+
post-type: blog
4+
by: Zelenya
5+
title: "Scala 3: My migration journey"
6+
description: I want to share my journey from Scala 2 to Scala 3 and discuss migration to Scala 3
7+
---
8+
9+
My name is Jay, I am a software engineer and developer advocate. We've recently migrated a major project from Scala 2 to Scala 3 at $WORK. In this article, I'd like to share the lessons we learned as well as some tips.
10+
11+
## Implicit context
12+
13+
Before doing any actual personal work, based on Internet discussions, I had the impression that switching to Scala 3 would be quite painful. In practice, it was pretty straightforward.
14+
15+
In retrospect, our experience was determined by the technology stack and tools that I (and my team) use: a microservice architecture, the Typelevel stack, VS Code with Metals, and, most importantly, no macros. It might not have been as smooth if any of these wasn't the case.
16+
17+
Let’s first explore our experience given that setup, follow the primary migration steps, and then we will talk about what to do if your setup and stack differ.
18+
19+
## Step 0. What does “migration” mean in your case?
20+
21+
Before you can jump into the action, you (and your team) have to answer a couple of questions.
22+
23+
If you are actively writing Scala, I’d expect that you don’t choose to stay on Scala 2. Why would anyone want to miss out on all the new features and improvements? Who wouldn’t want to rewrite everything to Scala 3? The reality is not so simple. Some people have monoliths, some use Spark ([which you can already run with Scala 3](https://xebia.com/blog/using-scala-3-with-spark/)), some have no time, and so on.
24+
25+
There is no one way to do the migration. Decide what’s possible and worthwhile to you (and your company).
26+
27+
Should you rewrite one service at a time? In what order? Or should you leave the old services untouched and write all the new services in Scala 3? What about internal libraries? How much time and resources can you allocate? The latter question might be the hardest.
28+
29+
It’s also an excellent time to discuss the benefits and trade-offs. Keep it fair. For example, if you just want a new syntax, is it worth disturbing an old service that hasn’t been touched in years? Well, it’s up to you to decide.
30+
31+
We decided to write all the new services in Scala 3 (where most of the future work will happen) while keeping the existing ones on Scala 2. Consequently, onboarding new developers without prior Scala experience is one of my biggest challenges. When they switch from a Scala 3 service to Scala 2, they bump into hurdles — some of the “intuitive” concepts don’t work as expected anymore. This requires more explanation; they seem to have to learn some things twice.
32+
33+
## Step 1. Analyze and plan
34+
35+
Even if we decide not to touch the existing services, we need to review our codebase:
36+
37+
- Do we have any shared internal libraries? Would we need to use them in new services?
38+
- What external libraries do we use? Do they have a Scala3-compatible release?
39+
40+
Other questions you might need to consider:
41+
42+
- How many services do you need to migrate?
43+
- What Scala version(s) does each service use?
44+
45+
Thanks to the [interoperability](https://docs.scala-lang.org/scala3/guides/migration/compatibility-source.html) between Scala 3 and Scala 2.13, it might be more accessible or crucial to upgrade to Scala 2.13 first. You have to keep this in mind.
46+
47+
*By this point, we knew that all our services were on 2.13; we knew how many internal libraries needed to be used from both Scala versions and what external libraries had no Scala 3 support: what the status is, what is missing, and what we can do to deal with it. For example, a couple of libraries got a Scala 3 release by the time we were done with the investigation, and one `circe` ”extras” library was replaced with two lines of boilerplate.*
48+
49+
## Step 2. Draw the rest of the owl
50+
51+
After all the meetings and/or write-ups comes the time to do the actual work. When you have concrete questions or action items, remember that [Scala 3 migration guides](https://docs.scala-lang.org/scala3/guides/migration/compatibility-intro.html) are your friends. For example, they cover how to port compiler options and build configurations. They are a great reference resource.
52+
53+
> Note that you can find further help from the community on the [Scala Discord](https://discord.com/invite/scala).
54+
55+
### Internal libraries
56+
57+
There are multiple ways of sharing a library between Scala 2 and Scala 3 services.
58+
59+
**The good news** is that you can use Scala 2.13 libraries from Scala 3 app and Scala 3 libraries from Scala 2.13 app.
60+
61+
**The bad news** is that you can’t do it directly if you have shared transitive dependencies.
62+
63+
**The good news** is that it’s pretty easy to deal with.
64+
65+
Imagine you have an internal library (e.g., authentication handling, a specific database connector, a peculiar API) used by your existing Scala 2 services that will also be used by Scala 3 services. If your library on Scala 2 uses the `ducks` library and your Scala 3 application uses `ducks` as well, you get two conflicting versions `ducks_2.13` and `ducks_3`.
66+
67+
To solve this, with sbt, you can cross-publish your library to both versions by adding one line to your build:
68+
69+
```scala
70+
+ crossScalaVersions := List("2.13.12", "3.3.1")
71+
```
72+
73+
To build against all versions listed in `crossScalaVersions`, prefix the action to run with `+`. For example:
74+
75+
```
76+
sbt> + compile
77+
```
78+
79+
*At least, that’s what we did — everything else just worked. Your mileage may vary.*
80+
81+
> For more information, see the guides on [ClassPath compatibility](https://docs.scala-lang.org/scala3/guides/migration/compatibility-classpath.html) and [Cross-building (sbt)](https://www.scala-sbt.org/1.x/docs/Cross-Build.html) or [Cross Builds (mill)](https://mill-build.com/mill/0.9.12/Cross_Builds.html)
82+
83+
### Dependencies
84+
85+
If all the libraries you use are actively maintained and published for Scala 3, there is nothing for you to worry about.
86+
87+
Otherwise, you might need [to help them](https://docs.scala-lang.org/contribute/) with migration or eliminate the dependency. Some libraries can be redundant on Scala 3 (because, for instance, Scala 3 has support for [Type Class Derivation](https://docs.scala-lang.org/scala3/reference/contextual/derivation.html)), and some have new alternatives.
88+
89+
*For example, at this stage, we’ve discovered [Iron](https://github.com/Iltotore/iron)[a great lightweight library](https://www.youtube.com/watch?v=4pq1elOap9k&ab_channel=ImpurePics) for type constraints, which replaced refined and newtype libraries in one go.*
90+
91+
### Plugins
92+
93+
Similarly to dependencies, some plugins support Scala 3, while some others are redundant and can be dropped.
94+
95+
The first plugin we were happy to retire was `better-monadic-for`, because Scala 3 already gives me better for-comprehensions. For example, we can define implicit values inside for-comprehensions:
96+
97+
```scala
98+
// Scala 3
99+
for
100+
given Logger[IO] <- makeLoggerOrSomething
101+
result <- doX
102+
yield doY(result)
103+
```
104+
105+
```scala
106+
// Scala 2 with better-monadic-for
107+
for
108+
implicit0(logger: Logger[IO]) <- makeLoggerOrSomething
109+
result <- doX
110+
yield doY(result)
111+
```
112+
113+
The second one is `kind-projector`. Scala 3 has [built-in type lambda syntax](https://docs.scala-lang.org/scala3/reference/new-types/type-lambdas-spec.html) and [kind-projector compatible syntax](https://docs.scala-lang.org/scala3/reference/changed-features/wildcards.html).
114+
115+
> For even more information, see the [Kind Projector migration guide](https://docs.scala-lang.org/scala3/guides/migration/plugin-kind-projector.html).
116+
117+
### Finally, code
118+
119+
Much of the Scala 2.13 code is still valid in Scala 3. If you want, you can use [the migration tools](https://docs.scala-lang.org/scala3/guides/migration/tooling-tour.html). Otherwise, if it sounds too boring, you can copy-paste the code over and fix whatever’s red.
120+
121+
> You can review the incompatibilities in the [Incompatibility Table](https://docs.scala-lang.org/scala3/guides/migration/incompatibility-table.html).
122+
123+
You might be wondering about all the new concepts and changes. For example, what should be done about `implicit` splits or enums? You don’t have to rewrite everything right away. Here and there, you can keep using the `implicit` keyword instead of `given` and `using`. The same applies to enums: you can keep using ADTs in the form of sealed traits with case classes and refactor to the new enums later.
124+
125+
However, it’s an exceptional opportunity to think about the style, patterns, and consistency. The code tends to replicate and spread around via copy-paste. You must decide whether to pay upfront or take shortcuts and do a more significant refactoring later.
126+
127+
Scala-3-rewrite allows you to get rid of the old workarounds as well as challenge and improve on your established Scala 2 patterns. *For instance, I was happy to try [error-handling](https://gvolpe.com/blog/error-handling-scala3/) with [union types](https://docs.scala-lang.org/scala3/book/types-union.html) and use new deriving mechanisms, but I’m still trying to find a more ergonomic usage for enums.*
128+
129+
## Side quests
130+
131+
### What to do about IDEs / IntelliJ IDEA
132+
133+
*I might not be the best person you should ask about IDEs, but I am still quite biased when it comes to tooling. I vividly remember a few years ago when the functional code (with `cats`) was primarily red. Also, I haven’t touched any Java IDEs in years; I mainly work with unpopular functional languages and am happy with syntax (plus error) highlighting and go-to-definitions. And if I don’t need to restart the LSP server a few times a day, I’m in heaven.*
134+
135+
To me, using VS Code with Metals and Scala 3 doesn’t feel different from using these with Scala 2 a couple of years ago. And it’s only getting better.
136+
137+
[IntelliJ IDEA 2023.2](https://blog.jetbrains.com/scala/2023/07/26/intellij-scala-plugin-2023-2-is-out/) brought enhanced Scala 3 support, and the team is constantly working on improvements ([IntelliJ IDEA 2023.3](https://blog.jetbrains.com/scala/2023/12/07/intellij-scala-plugin-2023-3-is-out/) was released just when I was writing this). [Metals](https://scalameta.org/metals/) has regular [releases](https://scalameta.org/metals/blog/) driven by the [Scala Center](https://scala.epfl.ch/)[VirtusLab](https://virtuslab.com/), and contributors from the community. So, if you haven’t recently tried either, now is a good time.
138+
139+
### What to do about macros (and other issues)
140+
141+
First, as discussed earlier, you should ask whether macros live in a separate service? Can you leave these services alone for now and rewrite them later? Or can you extract the usage of the macros into an independent service or a library?
142+
143+
If the macros rewrite is unavoidable but unattainable, consider asking for help. For instance, [VirtusLab](https://virtuslab.com/) offers [free support with migration](https://virtuslab.com/scala-3-support-and-migration/).
144+
145+
## Next steps
146+
147+
I hope this clears up and demystifies some things.
148+
149+
I’d also encourage you to share your migration stories even if they seem boring. Also, remember that the IntelliJ Scala plugin team asks for [feedback](https://blog.jetbrains.com/scala/2023/09/21/intellijscala-scala-days-2023-madrid/), and the [Scala Center](https://scala.epfl.ch/) regularly collects feedback.

0 commit comments

Comments
 (0)