Following up from my post on Deconstructing the map literal in Scala, I thought it might be fun to go the other way around, and create a few literals of own. Since Scala provides the language features to enable literal-like syntax, and not the actual literals themselves, we can do a lot of stuff to reduce the amount of typing we have to.
In your production code, you probably wouldn't use a lot of literals, regardless of language support. However, in test cases, they are much more common. If creating
objects in your tests becomes easier, you will tend to write better tests.
Suppose we are writing a Twitter app (that's all the rage these days, anyway). We might define a TwitterUser class like so:
We might write a few test cases:
and so forth. It's going to get a bit tedious writing new TwitterUser over and over again. We could just make a shorter-named method in our test class, but why not use the apply() sugar along with a singleton object to make our own literal?
It's a small bit of code, and we've saved some typing without sacrificing readability, since we take advantage of the way Twitter refers to users via the "@" symbol.
If our app is all about Twitter, we can go a step further and just rename TwitterUser to @@ like so:
This takes advantage of (the unfortunately named) case classes and now our Twitter user class and literal-like factory are called @@. This allows, among other things, some more readable case statements.
Now, you certainly don't want to do this for every object in your domain (you'd get a horrible mess of symbols), but for key objects that you are using everywhere, an appropriately chosen literal syntax can make your code very clean and easy to read and modify.