Understanding singleton types

A singleton type is just a data type that is designed to have a single instance. In Julia, it can be implemented easily by defining a type without any field:

struct OpenCommand end

To create a single instance of such a data type, we can use the following default constructor:

OpenCommand()

Unlike in some object-oriented programming languages, this constructor returns exactly the same instance, even if you call it multiple times. In other words, it is already a singleton. We can prove this like so:

After creating two instances of OpenCommand, we compare them using the === operator, which tells us that these two instances are indeed referring to the same object. Hence, we have achieved the creation of a singleton.

Moving on, we can take the same approach and create a singleton types for each command, that is, CloseCommand, ExitCommand, HelpCommand, and so on. Furthermore, we can also create a new abstract type called AbstractCommand, which can serve as the supertype for all these command types.

It seems exhaustively verbose that we have to create a new type for each command. A better way to handle this situation is to use parametric types. Since it's a fairly common use case, Julia predefines a type called Val. Let's take a look at it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.143.4