Another tour of Scala

 
 
 
 

Scala Object

The Gist

The Scala Tour doesn’t officially call out this feature.

My Interpretation

In Scala, classes have no static members (as they can in Java and Ruby). Instead, Scala provides a means to create singleton objects. These are, essentially, global variables (although it is only global to the scope in which it is included via import). However, by judicious use of naming, these objects can make your code more readable, especially with respect to PatternMatching.

In the most basic case, you can use objects as a means to “statically import” methods to avoid full-qualification:

This is pretty much static imports from Java. Note a few things:

  • We can import pretty much anywhere in our code; we don’t need to import for the entire file — this is handy for keeping code comprehensible and reducing data flow
  • This is not canonical Scala code; you don’t frequently throw exceptions or deal with the option class in this way.

Objects in Scala serve a more complex and important purpose than as an alternate syntax for static imports, however. AdvancedScalaObjects explains this in more detail.

My Thoughts on This Feature

I never cared for static imports in Java; outside of test code it seemed to make code less readable, because it increased the size of the global symbol space. This is still an issue for Scala, however the ability to import these symbols at any scope virtually eliminates the negative aspects.

That being said, I’m not sure why these couldn’t just be formally defined as class methods; I’m not clear on what is gained by having these be part of another object. See AdvancedScalaObjects for more detail on this (which will make more sense after reading that entry).


Last Updated 09/15/2011 at 07:23:13 PM by davec

blog comments powered by Disqus