primary constructor

How to call a Scala superclass constructor

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 4.11, “How to call a Scala superclass constructor.”

Problem

You want to control a Scala superclass constructor that’s called when you create constructors in a subclass.

How to define a private primary constructor in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 4.4, “How to define a private primary constructor in Scala.”

Problem

You want to make the primary constructor of a Scala class private, such as to enforce the Singleton pattern.

Solution

To make the primary constructor private, insert the private keyword in between the class name and any parameters the constructor accepts:

How to create a primary constructor in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 4.1, “How to create a primary constructor in Scala.”

Problem

You want to create a primary constructor for a class, and you quickly find that the approach is different than Java.

Solution

The primary constructor of a Scala class is a combination of:

Scala FAQ - How to create a private default/primary constructor

I haven't had the need for this syntax yet, but I just saw some code that shows how to create a private primary constructor in Scala, and I thought I'd test it and share an example of it.

In the example below, I've made the primary constructor of the Order class private: