Dodo doc > Types > Constructors and Methods > Constructors


Constructors

Constructors are functions which role is to create a new instance of the type.

Syntax

Constructor names are the same as the type name or Self can be used instead. This means they follow the same syntax rules as type names.

Declaration

A constructor declaration looks similar to a function declaration without a return type where "def" is replaced with "make". It must always be part of a type declaration.

If a type has a constructor without arguments, dodo uses it to create a type member called "instance" when the module loads. It is used as default value of the type. If a type has no constructor, dodo uses the default value of its attributes to create the default instance.

If the type constructors all require at least one argument then the type has no default value. This means variable declarations of this type must always have an initialisation value.

When the constructor declaration contains the type name, and the type doesn't extend another type or have any features, then the type name is optional in the type declaration.

Constructor parameters can be passed by reference. This is noted with a "&" prefix before the parameter type.

Examples:

class House
{
   make House(Colour?, Integer?)
   make Self(&Directory)
   make Self(Plans)
}

button:
   make RoundButton(Double)
   Double width
.       no default value for RoundButton

Note: In the first example, the default value is created using the first constructor as both arguments are optional.

Rationale: constructors offer an alternative way to create new instances of a type when using prototypes is not adequate. Default values ensure that all variables are always initialised. If the type has constructors but none accepts zero arguments, it is assumed the programmer does not want a default value to be created when the module loads.

Definition

A constructor definition is similar to a function definition with a block body.

In the constructor, a variable called "self" which is the newly created instance is declared. Type members are also declared. Instance members are not declared.

As a special rule, Self or Super in a constructor body calls the matching constructor defined in the type or supertype respectively and acts on "self", not on yet another instance of the type or supertype. A supertype constructor can only be invoked if "self" is compatible with the instances of the supertype.

Example:

make House(&Directory dir)
{
   Self()
   dir.Register(*self)
}

Rationale: The constructor is considered a type member which is why it needs to access instance members through "self" and why it can access other type members. The special rule on constructor calls allows constructor chaining.

Invocation

A constructor call takes arguments and returns the newly created object.

Unlike a function call, the brackets are required even if the argument list is empty.

Variable references need to be passed with a dot "." in front of the variable name. There should be no space between the dot and the variable.

Examples:

House()
House(.dir)

Note: a constructor invocation is never preceded with "new". In dodo the "new" keyword is used to create a new variable from a prototype (see section on variable use as prototype). We will see a use for this invocation style in the section on methods.

Rationale: since a constructor name is a type name it is important to include the brackets after it to distinguish a constructor call from a type.
Although the format of constructor calls departs from other languages such as Java which requires them to be preceded with "new", the dodo syntax is easy and less verbose.
Passing a reference, on the other hand, uses more characters in dodo. The dot is used to document the fact that the call can modify the variable. It also forces the call to be changed if the signature of the constructor changes to take a reference, which can avoid programming errors.


^ 4.6. Constructors and Methods

v 4.6.2. Instance Methods