Explanation of singleton objects in Scala -


i coding in provide "object someclass" , "class someclass" , companion class class declaration , object singleton. of cannot create instance. so... question purpose of singleton object in particular instance.

is way provide class methods in scala? + based methods in objective-c?

i'm reading programming in scala book , chapter 4 talked singleton objects, doesn't lot of detail on why matters.

i realize may getting ahead of myself here , might explained in greater detail later. if so, let me know. book reasonably far, has lot of "in java, this", have little java experience sort of miss bit of points fear. don't want 1 of situations.

i don't recall reading anywhere on programming in scala website java prerequisite reading book...

yes, companion singletons provide equivalent java's (and c++'s, c#'s, etc.) static methods.

(indeed, companion object methods exposed via "static forwarders" sake of java interop)

however, singletons go fair way beyond this.

  • a singleton can inherit methods other classes/traits, can't done statics.
  • a singleton can passed parameter (perhaps via inherited interface)
  • a singleton can exist within scope of surrounding class or method, java can have inner classes
  • it's worth noting singleton doesn't have companion, it's valid define singleton without defining companion class.

which helps make scala far more object-oriented language java (static methods don't belong object). ironic, given it's largely discussed in terms of functional credentials.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -