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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
For Scala 2, use
scala.util.Properties.versionNumberString(orversionString):For Scala 3, if you do the same thing, you may be surprised by the answer:
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:
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:
Here, the choice of
scala.CanEqualis 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.