As my learning of Scala continues, I have been intrigued by some of the choices in the Scala language. Consider the removal of static classes. In Java world (from where I come), there is a distinct difference between a static member, a singleton and a instance member. There was always a persistent need for a singleton in Java which a static member could not really help with. The primary use cases I know for why a singleton may be preferred over a static member are:
- Ability to control the instantiation of an singleton object. If loading the instance of a class is resource heavy, we want to push it off for later till it is actually needed.
- Ability to configure a singleton object at runtime. Imagine having to read environment variables and populate our singleton at construction time. This cannot be done if the member is static since the information may not be known at class loading time.
It appears however that Scala's implementation of singleton will be devoid of the above benefits. Look at the discussion here: http://pbadenski.blogspot.com/2009/06/design-patterns-in-scala-singleton.html
It appears to me that does not Scala solve the singleton use cases at all. Which would be a disappointment.
If my understanding is correct then the next question is: How do we enable a lazy singleton pattern in Scala?
Seems like we have to fight scala to get singleton the correct way!
PS: This is not my blog
objectsbeing during class load time.