Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve Scala's version in REPL? [duplicate]

Tags:

scala

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?

like image 917
soc Avatar asked Aug 06 '11 15:08

soc


People also ask

How do I find Scala version?

Check Scala Version Using scala Command Write the scala command to your terminal and press enter. After that, it opens Scala interpreter with a welcome message and Scala version and JVM details.

How do I check my Jupyter version in Scala?

Click on Admin -> Stack and Versions and you will find the version information under Version tab. Using Scala version 2.10.

How do I know if Scala is installed on Linux?

Type scala -version in your terminal. If the output shows that Scala 2.11. 1 (or higher) is installed, then you are done.

Does Scala have a REPL?

The Scala REPL is a tool (scala) for evaluating expressions in Scala. The scala command will execute a source script by wrapping it in a template and then compiling and executing the resulting program.


4 Answers

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.

like image 61
Daniel C. Sobral Avatar answered Oct 10 '22 22:10

Daniel C. Sobral


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.

like image 20
Debilski Avatar answered Oct 10 '22 23:10

Debilski


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
like image 24
Can Avatar answered Oct 10 '22 22:10

Can


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

like image 44
Dmitrii Avatar answered Oct 11 '22 00:10

Dmitrii