Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Objects and the rise of singletons

Tags:

General style question.

As I become better at writing functional code, more of my methods are becoming pure functions. I find that lots of my "classes" (in the loose sense of a container of code) are becoming state free. Therefore I make them objects instead of classes as there is no need to instantiate them.

Now in the Java world, having a class full of "static" methods would seem rather odd, and is generally only used for "helper" classes, like you see with Guava and Commons-* and so on.

So my question is, in the Scala world, is having lots of logic inside "objects" and not "classes" quite normal, or is there another preferred idiom.

like image 555
sksamuel Avatar asked Nov 30 '12 13:11

sksamuel


People also ask

Are Scala objects singletons?

Instead of static keyword Scala has singleton object. A Singleton object is an object which defines a single object of a class. A singleton object provides an entry point to your program execution. If you do not create a singleton object in your program, then your code compile successfully but does not give output.

What is the advantage of companion objects in Scala?

Advantages of Companion Objects in Scala Companion objects provide a clear separation between static and non-static methods in a class because everything that is located inside a companion object is not a part of the class's runtime objects but is available from a static context and vice versa.

What is the necessity for singleton and companion objects?

If you don't create singleton object, your code will compile successfully but will not produce any output. Methods declared inside Singleton Object are accessible globally. A singleton object can extend classes and traits.

What is the difference between singleton object and companion object?

A singleton object named the same as a class is called a companion object. Also a companion object must be defined inside the same source file as the class.


2 Answers

As you mention in your title, objects are singleton classes, not classes with static methods as you mention in the text of your question.

And there are a few things that make scala objects better than both static AND singletons in java-world, so it is quite "normal" to use them in scala.

For one thing, unlike static methods, object methods are polymorphic, so you can easily inject objects as dependencies:

scala> trait Quack {def quack="quack"}
defined trait Quack

scala> class Duck extends Quack
defined class Duck

scala> object Quacker extends Quack {override def quack="QUAACK"}
defined module Quacker

// MakeItQuack expects something implementing Quack
scala> def MakeItQuack(q: Quack) = q.quack
MakeItQuack: (q: Quack)java.lang.String

// ...it can be a class
scala> MakeItQuack(new Duck)
res0: java.lang.String = quack

// ...or it can be an object
scala> MakeItQuack(Quacker)
res1: java.lang.String = QUAACK

This makes them usable without tight coupling and without promoting global state (which are two of the issues generally attributed to both static methods and singletons).

Then there's the fact that they do away with all the boilerplate that makes singletons so ugly and unidiomatic-looking in java. This is an often overlooked point, in my opinion, and part of what makes singletons so frowned upon in java even when they are stateless and not used as global state.

Also, the boilerplate you have to repeat in all java singletons gives the class two responsibilities: ensuring there's only one instance of itself and doing whatever it's supposed to do. The fact that scala has a declarative way of specifying that something is a singleton relieves the class and the programmer from breaking the single responsibility principle. In scala you know an object is a singleton and you can just reason about what it does.

like image 59
Paolo Falabella Avatar answered Sep 23 '22 18:09

Paolo Falabella


You can also use package objects e.g. take a look at the scala.math package object here https://lampsvn.epfl.ch/trac/scala/browser/scala/tags/R_2_9_1_final/src//library/scala/math/package.scala

like image 31
SpiderPig Avatar answered Sep 25 '22 18:09

SpiderPig