Primary Constructors: Things a C# Developer Can Love About Scala

27 April 2015

Introduction

Continuing the theme of learning Scala in the open, I'm going to talk today about a part of Scala I've only recently become aware of and contrast it to it's C# counterparts. Over time, I plan to release a series of these posts, as I learn more about Scala and find more things to love about the language.

Primary constructors are often mooted for C#'s future, but at this point in time, C# primary constructors are yet to have a fully specified implementation. In Scala, primary constructors are the key way to build a class. Rather than try to describe a primary constructor, let's look at an example.

The primary constructor of a class in Scala forms part of the class definition. In the class' primary constructor, parameters can be defined in one of three ways:

Value Parameters

A primary constructor parameter preceded by the val keyword is defined as an immutable class property, which means that while you can read (get in C#) a val, you cannot change (set in C#) the value of a val. I'll speak about the benefits of immutability vs mutability in a future blog post, but for now, keep in mind that immutability is generally preferred in most functional languages, including Scala.

Here's a simple Scala class that uses a value primary constructor parameter:

class Circle(val radius: Int)

The C# equivalent to the value primary constructor parameter would be a read only public property with a private readonly backing field set via a constructor parameter as shown below.

public class Circle {
    
    private readonly double _radius;

    public double Radius { get { return _radius; } }

    public Circle(int radius)
    {
        _radius = radius;
    }
}

You can immediately see that the Scala version, once you understand the meaning of the keywords, is much more succinct than the C# version.

Variable Parameters

A primary constructor parameter preceded by the var keyword is defined as a mutable class property, which can be read and changed. Generally it is considered best to use val by default, however there are occasions in which mutability is required.

Here's a simple Scala class that uses a variable primary constructor parameter:

class Circle(var radius: Int)

The C# equivalent to the var constructor parameter would be the non read only public property set via a constructor parameter as shown below.

public class Circle {
    
    public double Radius { get; set; }

    public Circle(int radius)
    {
        Radius = radius;
    }
}

No Prefix Parameters

A parameter not preceded by any prefix is simply a variable into the constructor that is not visible outside the scope of the constructor.

Here's a simple Scala class that uses a no prefix primary constructor parameter:

class Circle(radius: Int) {
    val area = math.Pi * radius * radius
}

The C# equivalent to the no prefix constructor parameter would simply be a constructor variable not assigned to any fields/properties.

public class Circle {
    
    public double Area { get; private set; }

    public Circle(int radius)
    {
        Area = Math.Pi * radius * radius;
    }
}

In Conclusion

There are a couple of reasons why I'm such a big fan of the primary constructor in Scala. Firstly, I'm a big fan of the guideline that classes should not be able to be constructed without all of their required dependencies. That is, a consumer should not be able to set up a class in a bad state. While Scala certainly doesn't stop you from building a class that can be set up without all that it requires, it's certainly more opinionated about it, gently nudging you in the right direction by making the class definition describe dependencies as part of its definition.

The second reason I'm a fan is the simple succinctness of the Scala primary constructor. When you compare the Scala constructor to the C# ones, Scala is a lot more succinct without, in my opinion, losing any of its clarity.

I've got a few ideas for future blog posts on things a C# developer can love about Scala, and I'm sure to come up with many more as I go, so stay tuned for next time!

Tags: C# | Scala
Tweet