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”
Sorry, comments are closed for this article.



September 14th, 2011 at 09:21 AM
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.
September 14th, 2011 at 09:22 AM
One more example from the Bowler web framework to define routes: http://bowlerframework.org/manual.html#functionnameconventionroutes
E.
September 14th, 2011 at 11:33 AM
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
September 14th, 2011 at 09:17 PM
These are great examples! Certainly you can get carried with a trick like this, but like any idea, the benefits are sometimes worth it.
October 5th, 2011 at 10:25 PM
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
October 5th, 2011 at 10:26 PM
I thought, “wouldn’t it be great if that name got printed with spaces instead, so it’s more readable?