Incremental conversion to Scala 3

I’m thinking about trying to convert some of my Scala 2.13.4 code to Scala 3, but I am a bit confused. Can I do that conversion one source file at a time and still have the whole application work? Or are there certain Scala 2 features that prevent that for source files that use them? If so, what are those features? And where can I find step-by-step instructions? Thanks.

1 Like

You can not use a different compiler for a single file, this wasn’t even true for minor versions like 2.12 to 2.13 (although it should be possible for patch versions, but who would do that in any case). And AFAIK, there is no language that provides that.

However, most Scala 2 code should be compilable by Scala 3, so you can just change the compiler and see what happens, if there are no errors then you can work in the warnings first and then start using new features as you please.

1 Like

OK, thanks. My build.sbt file contains this line:

scalaVersion := “2.13.4”

Can I just change that version number to 3.something? If so, 3.what?

And can I easily revert back by just changing it back to 2.13.4?

Yeah to 3.0.0-M3 and yeah you can revert back by putting the old version.

1 Like
3 Likes

I changed “2.13.4” to “3.0.0-M3” in build.sbt, and I got this:

[error] (update) sbt.librarymanagement.ResolveException: unresolved dependency: org.scala-lang#scala-library;3.0.0-M1: not found
[error] unresolved dependency: org.scala-lang.modules#scala-xml_3.0.0-M1;1.2.0: not found
[error] unresolved dependency: org.scala-lang#scala-compiler;3.0.0-M1: not found

Now what?

Since 2.0.0-M3, Scala 3 is supported.

1 Like

Well, as with any other version change you need to also change (generally update) your libraries versions to one that works with the new Scala version you are using.

You can find that by looking at the documentation of each library or searching in mavencentral.

Also, in this case, you can use the withDottyCompat (but do it with care).
Take a look to this.

1 Like

Did you add sbt-dotty to your project/plugins.sbt?

1 Like

I now see that I have two build.properties files:

toplevel/project/build.properties
toplevel/src/project/build.properties

Should I delete one of these?

Put this in toplevel/project/build.properties
sbt.version=1.4.5

And add this in toplevel/project/plugins.sbt
addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % "0.5.1")
(This last step wont be needed in later version (not in RC1 i hope ???))

And then scalaVersion := "3.0.0-M3" in toplevel/build.sbt should just work…

2 Likes

(you can track progress on that at Merge DottyPlugin in sbt · Issue #6080 · sbt/sbt · GitHub)

2 Likes

I don’t know about those files, but I find it unusual to have a project folder inside the src folder.

2 Likes

You can delete that one.

1 Like

Yes, typically that kind of thing happens when I run a command from the wrong directory.

My build.sbt has this:

libraryDependencies += “org.scala-lang.modules” %% “scala-xml” % “1.2.0”
libraryDependencies +=
“org.scala-lang.modules” %% “scala-parallel-collections” % “1.0.0”

and I am getting this error message:

[error] not found: /home/rpaielli/.ivy2/localorg.scala-lang.modules/scala-xml_3.0.0-M1/1.2.0/ivys/ivy.xml
[error] not found: https://repo1.maven.org/maven2/org/scala-lang/modules/scala-xml_3.0.0-M1/1.2.0/scala-xml_3.0.0-M1-1.2.0.pom

What do I need to change library dependencies to? Thanks.

Scaladex (https://index.scala-lang.org) is a good tool for answering these questions.

Entering “scala-xml” into the search bar will lead you to https://index.scala-lang.org/scala/scala-xml/scala-xml , and from there if you click the “Version Matrix” button it’ll take you to https://index.scala-lang.org/artifacts/scala/scala-xml , where you can see that the only scala-xml version available for 3.0.0-M2 and 3.0.0-M3 is 2.0.0-M3.

But also, the error message you’re getting indicates that you’re actually trying to use 3.0.0-M1. Earlier you said you were trying to use 3.0.0-M3, so you probably want to sort that out first. Do you have a lingering reference to 3.0.0-M1 somewhere?

4 Likes

Thanks, Seth. That got me unstuck.

1 Like