24

When trying to run some code in online interpreters or with IRC bots, I always wonder which version of Scala they support.

Is there a way to retrieve the version of Scala from within the interpreter?

Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
soc
  • 27,983
  • 20
  • 111
  • 215
  • 3
    Does this answer your question? [How do I get the Scala version from within Scala itself?](https://stackoverflow.com/questions/6121403/how-do-i-get-the-scala-version-from-within-scala-itself) – Alex Archambault Sep 14 '21 at 10:20
  • This question is 10 years old and I left Scala half a decade ago? But yes, both questions have correct answers. – soc Sep 21 '21 at 16:18
  • I actually didn't write that comment myself, I only suggested a duplicate via the SO UI ^^ (and it seems SO generated the comment). – Alex Archambault Sep 27 '21 at 08:42

4 Answers4

43

For Scala 2, use scala.util.Properties.versionNumberString (or versionString):

scala> scala.util.Properties.versionString
val res0: String = version 2.13.6

scala> scala.util.Properties.versionNumberString
val res1: String = 2.13.6

For Scala 3, if you do the same thing, you may be surprised by the answer:

% scala3 -version
Scala code runner version 3.0.1 -- Copyright 2002-2021, LAMP/EPFL
% scala3
scala> scala.util.Properties.versionNumberString                                                                              
val res0: String = 2.13.6

That's because Scala 3.0.x uses the Scala 2 standard library as-is, to aid migration, and makes only a small number of additions. (Eventually the standard libraries will no longer remain synchronized like this.)

Here's how to get the Scala 3 compiler version:

scala> dotty.tools.dotc.config.Properties.simpleVersionString
val res0: String = 3.0.1

This only works if the scala3-compiler JAR is on your classpath. (In the standard Scala 3 REPL, it is; in some other environments, it might not be.)

If the compiler isn't on your classpath and you want the full Scala 3 version string, see Dmitrii's answer.

If the compiler isn't on your classpath but you just want to find out at runtime whether you're on Scala 2 or 3, well... perhaps there's a cleaner/better way, you tell me, but one way that works is:

util.Try(Class.forName("scala.CanEqual")).isSuccess

Here, the choice of scala.CanEqual is arbitrary, it could be any of the small number of classes that are in scala3-library but not scala-library.

But if you are tempted to go that route, you might instead consider including version-specific source in your project, or passing the Scala version via sbt-buildinfo.

Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
6
scala> scala.util.Properties.versionMsg
res: String = Scala library version 2.9.0.1 -- Copyright 2002-2011, LAMP/EPFL

Looks of course like the library version and not like the language version, but I think currently there won’t be a real difference in practice.

Debilski
  • 66,976
  • 12
  • 110
  • 133
5

If you need just the version number without the "version" keyword you can use versionNumberString function.

scala> scala.util.Properties.versionNumberString
res1: String = 2.12.3
Can
  • 599
  • 4
  • 13
5

If you want to get the exact Scala 3 version, you can read it from the Manifest file
.../scala3-library_3-3.0.1.jar!/META-INF/MANIFEST.MF

import java.io.FileInputStream
import java.util.jar.JarInputStream

val scala3LibJar = classOf[CanEqual[_, _]].getProtectionDomain.getCodeSource.getLocation.toURI.getPath
val manifest = new JarInputStream(new FileInputStream(scala3LibJar)).getManifest
manifest.getMainAttributes.getValue("Implementation-Version")

Example in Scastie:

enter image description here

Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
Dmitrii
  • 340
  • 2
  • 6