4

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:

  1. 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.
  2. 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

6
  • What is "the singleton problem again?" Commented Aug 8, 2013 at 2:34
  • Besides, I've been using Scala since 2008 and don't ever remember the instantiation of objects being during class load time. Commented Aug 8, 2013 at 2:42
  • I like your question (it draws out some good answers) BUT I won't up-vote it because - as written - it tries to answer itself and I don't agree with your conclusion. Commented Aug 8, 2013 at 5:10
  • Note also that the Scala "static portion of the class" is the singleton "object". It's a true object: you can reference it, it can inherit traits/interfaces... so if you WANT "different" implementations from some factory, the factory can certainly return references to different "objects". Commented Aug 8, 2013 at 5:13
  • 2
    The statements in this question in the blog post are just false. Commented Aug 8, 2013 at 5:44

2 Answers 2

22

Singletons in Scala are lazy. Try the following in your REPL:

scala> object Foo { println("hello") }
defined module Foo

scala> 1+1
res0: Int = 2

scala> Foo
hello
res1: Foo.type = Foo$@37c8ccf4

scala> Foo
res2: Foo.type = Foo$@37c8ccf4

As you can see from the println, Foo isn't initialized until it's used.

Sign up to request clarification or add additional context in comments.

Comments

0

TOUR OF SCALA SINGLETON OBJECTS

An object is a class that has exactly one instance. It is created lazily when it is referenced, like a lazy val.

As a member of an enclosing class or as a local value, it behaves exactly like a lazy val.

Below is the demo

scala> object Foo { println("hello") }
defined object Foo

scala> val f = Foo
hello
f: Foo.type = Foo$@2e26173

scala> object Foo { println("hello") }
defined object Foo

scala> lazy val f = Foo
f: Foo.type = <lazy>

scala> f
hello
res0: Foo.type = Foo$@7bf018dd

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.