Scala Identifiers with Spaces

September 14th, 2011

This morning, I was looking at some Java code I’m writing for a client. I have an enumeration of flags for various error conditions, say for example, FileHasTheWrongFormat. As I looked at a log file, I thought, “wouldn’t it be great if that name got printed with spaces instead, so it’s more readable?” Of course, you can add an appropriate toString method, but then I remembered Scala’s back quote syntax, e.g., java.util.Scanner.`match`, which lets you use Java identifiers that are reserved words in Scala (like match in this case). Could you use back quotes to embed spaces in identifiers?

This morning, I was looking at some Java code I’m writing for a client. I have an enumeration of flags for various error conditions, say for example, NoFileInThatDirectory. As I looked at a log file, I thought, “wouldn’t it be great if that name got printed with spaces instead, so it’s more readable?” Of course, you can add an appropriate toString method, but then I remembered Scala’s back quote syntax, e.g., java.util.Scanner.`match`, which lets you use Java identifiers that are reserved words in Scala (like match in this case). Could you use back quotes to embed spaces in identifiers?

It works!


scala> case class `My Class Has Spaces`(`some int`: Int)
defined class My$u0020Class$u0020Has$u0020Spaces

scala> val `a value`=`My Class Has Spaces`(1)
a value: My Class Has Spaces = My Class Has Spaces(1)

scala> println(`a value`)
My Class Has Spaces(1)

scala> case class `My Exception` extends RuntimeException
defined class My$u0020Exception

scala> throw `My Exception`()
My$u0020Exception
        at .<init>(<console>:10)
        at .<clinit>(<console>)
...

Why would you do this? Well, it could be useful for DSLs and for error messages, like my motivating example. Unfortunately, the nice name isn’t always used, as in the stack trace and the result printed after defining the class, so it won’t always work well and it could get tedious typing those quotes.

Still, it’s your Scala trivia of the day! You’re welcome ;)

6 Responses to “Scala Identifiers with Spaces”

  1. Eric Torreborre Says:

    Hi Dean,

    @codahale has a project called “simplespec” (built on top of specs) using this quotation trick to write test methods: https://github.com/codahale/simplespec. I guess you should be able to do that with classic JUnit test in Scala as well.

    And in specs2, I show that you can also use this idea to insert examples into an acceptance specification: http://etorreborre.github.com/specs2/guide/org.specs2.guide.SpecStructure.html#Acceptance+specification.

    Eric.

  2. Eric Torreborre Says:

    One more example from the Bowler web framework to define routes: http://bowlerframework.org/manual.html#functionnameconventionroutes

    E.

  3. Ittay Dror Says:

    I used it so compile errors will contain a hint about the reason of the error. See: http://www.tikalk.com/java/blog/avoiding-nothing

  4. Dean Wampler Says:

    These are great examples! Certainly you can get carried with a trick like this, but like any idea, the benefits are sometimes worth it.

  5. power balance wholesale Says:

    I was looking at some Java code I’m writing for a client. I have an enumeration of flags for various error conditions, say for example

  6. power balance Says:

    I thought, “wouldn’t it be great if that name got printed with spaces instead, so it’s more readable?

Sorry, comments are closed for this article.