Lazy Singleton in Scala -


as learning of scala continues, have been intrigued of choices in scala language. consider removal of static classes. in java world (from come), there distinct difference between static member, singleton , instance member. there persistent need singleton in java static member not with. primary use cases know why singleton may preferred on static member are:

  1. ability control instantiation of singleton object. if loading instance of class resource heavy, want push off later till needed.
  2. ability configure singleton object @ runtime. imagine having read environment variables , populate our singleton @ construction time. cannot done if member static since information may not known @ class loading time.

it appears scala's implementation of singleton devoid of above benefits. @ discussion here: http://pbadenski.blogspot.com/2009/06/design-patterns-in-scala-singleton.html

it appears me not scala solve singleton use cases @ all. disappointment.

if understanding correct next question is: how enable lazy singleton pattern in scala?

seems have fight scala singleton correct way!

ps: not blog

singletons in scala lazy. try following in 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 can see println, foo isn't initialized until it's used.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -