Saturday, April 16, 2011

Detect Scala version at run-time

Thanks to Josh Suereth for this one.

Update 1: add stainsby's improvement.
Update 2: add Ismael Juma's improvement.
Update 3: add Samuel Tardieu's improvement.

Can’t see the code?

8 comments:

  1. Less mess to use a Java Properties object:

    scala> val props = new java.util.Properties()
    props: java.util.Properties = {}

    scala> props.load(getClass.getResourceAsStream("/library.properties"))

    scala> props.getProperty("version.number")
    res5: java.lang.String = 2.8.1.final

    ReplyDelete
  2. Thanks stainsby! That is indeed less messy. I'll update the post.

    ReplyDelete
  3. Hi,

    How about:

    scala> util.Properties.versionString
    res2: java.lang.String = version 2.9.0.RC1

    Best,
    Ismael

    ReplyDelete
  4. How about:

    scala> scala.util.Properties.releaseVersion
    res0: Option[String] = None

    scala> scala.util.Properties.developmentVersion
    res1: Option[String] = Some(r24776)

    It will help to be using at least oh r24776 or so.

    ReplyDelete
  5. Shoot, now I notice I blew that commit. Will revise.

    ReplyDelete
  6. Well, I'm still not happy with it but at least it makes something like sense. Not sure what the right spot is between too little done and too much. But I do know it's ridiculous the only way to get the version was as part of a string which had had "version " prepended.

    ReplyDelete
  7. Functions that return Option[T] are often complemented by a function that accepts a (=>T) and returns T by composing with getOrElse.

    ReplyDelete