Scala 3 is going to make Scala more easy to write, and especially read. more power features like enums and less misleading keywords like implicit.
But first we need to migrate our old Scala 2.12 / 2.13 codebase to Scala 3.
This talk tells the story of how I tried to migrate greyhound open-source library to Scala 3 with partial success.
You will hear about what works, what doesn't and about a few pitfalls to avoid
Migration takeaways include:
1. use migration tools, don't do it manually
2. which popular 3rd party libraries can and can't be used by Scala 3 code
and many more
2. @NSilnitsky
Migrating a library to Scala 3
Greyhound
A Scala/Java high-level SDK
for Apache Kafka.
Powered by ZIO
Takeaways from
migrating a library
5. SBT
Migrating a library to Scala 3
Scala 2.12
Bazel
Scala
2.13
Scala
3.1
Migrating Greyhound library
6. Migrating a library to Scala 3
SBT
Scala 2.12
Bazel
Scala
2.13
Scala
3.1
Migrating Greyhound library
Step 1:
Make SBT Work
7. Migrating a library to Scala 3
Bazel does not support
Scala 3 yet.
And SBT has…
→ a Nice Migration Plugin
→ dedicated syntax
SBT
Scala 2.12
Bazel
Why?
Step 1:
Make SBT Work
23. @NSilnitsky
@NSilnitsky
val offsets = records.groupBy(RecordTopicPartition(_)).mapValues(_.maxBy(_.offset).offset + 1)
val offsets = records.groupBy(RecordTopicPartition(_)).mapValues(...).toMap
> scalafix Collection213Roughly
[info] Running scalafix on 69 Scala sources
[success] Total time: 24 s, completed 16 Feb 2022, 22:22:18
Ended up fixing it manually:
24. Migrating a library to Scala 3
SBT
Scala 2.12
Bazel
Scala
2.13
Scala
3.1
Step 1:
Make SBT Work
Step 2:
Switch to Scala 2.13
Step 3:
Switch to Scala 3
25. from
Scala 2.13
to
Scala 3 Scala 3
compiler
Scala 2.13
compiler
3rd-party
library dep
Scala 2.13 TASTy Reader can read TASTy format
Scala3 Unpickler
Classpath
Interoperability
can read Pickle format
26. Just use the migration tool
from
Scala 2.13
to
Scala 3
SBT migration
plugin
27. @NSilnitsky
helps you migrate your build & code
to Scala3.
Use SBT Migration Plugin.
Migrating a library to Scala 3
👉 addSbtPlugin("ch.epfl.scala" % "sbt-scala3-migrate" % "0.5.0")
migrate-libs
migrate-scalacOptions
migrate-syntax
migrate
31. @NSilnitsky
> migrate-syntax root
Migrating a library to Scala 3
These changes are not mandatory, and caused many new 2.13 compiler
errors → Rolled back
[error] /…/src/main/scala/future-interop/com/wixpress/dst/greyhound/future/GreyhoundRuntime.scala:27:19: type mismatch;
[error] found : zio.clock.Clock.Service.live.type (with underlying type zio.clock.Clock.Service)
[error] required: zio.blocking.Blocking.Service with zio.random.Random.Service with zio.system.System.Service with
zio.console.Console.Service with zio.clock.Clock.Service
[error] Clock.Service.live,
[error] ^
val zenv: Has[Clock.Service] with Has[Console.Service] with Has[System.Service] with Has[Random.Service] with
Has[Blocking.Service]
32. compiles in scala 3 with options:
> migrate root
Migrating a library to Scala 3
-source:3.0-migration
-rewrite
34. @NSilnitsky
@NSilnitsky
rewrite mode:
Once your code compiles in the migration mode,
Warnings are resolved by the compiler itself
scalacOptions ++= Seq(
"-source:3.0-migration", “-rewrite”
),
➔ Rewrites are NOT applied if the code compiles in error.
➔ You cannot choose which rules are applied (compiler runs all of them).
Scala compiler options
35. @NSilnitsky
> migrate root
Migrating a library to Scala 3
[warn]
[warn] Note: Unresolved dependencies path:
[error] stack trace is suppressed; run last update for the full output
[error] (update) sbt.librarymanagement.ResolveException: Error downloading org.scalameta:semanticdb-scalac_2.13.8:4.4.31
[error] Not found
[error] Not found
[error] not found: /Users/natans/.ivy2/localorg.scalameta/semanticdb-scalac_2.13.8/4.4.31/ivys/ivy.xml
[error] not found:
https://repo1.maven.org/maven2/org/scalameta/semanticdb-scalac_2.13.8/4.4.31/semanticdb-scalac_2.13.8-4.4.31.pom
36. @NSilnitsky
> migrate root
Migrating a library to Scala 3
[warn]
[warn] Note: Unresolved dependencies path:
[error] stack trace is suppressed; run last update for the full output
[error] (update) sbt.librarymanagement.ResolveException: Error downloading org.scalameta:semanticdb-scalac_2.13.8:4.4.31
[error] Not found
[error] Not found
[error] not found: /Users/natans/.ivy2/localorg.scalameta/semanticdb-scalac_2.13.8/4.4.31/ivys/ivy.xml
[error] not found:
https://repo1.maven.org/maven2/org/scalameta/semanticdb-scalac_2.13.8/4.4.31/semanticdb-scalac_2.13.8-4.4.31.pom
Failed for 2.13.8
41. @NSilnitsky
5 other build tools for support
➔ Bazel - work in progress - ETA - end of Q1
➔ Gradle - Scala 3 supported version 7.3
➔ Mill - support in 0.9.x
➔ Maven - 4.5.x (scala-maven-plugin)
➔ Native Cross compilation for your library
Migrating a library to Scala 3