Dodo doc > Types > Constructors and Methods > Constructors


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


Constructors must have the same name as the type name. This means they follow the same syntax rules.

If the type has no name, then an underscore "_" can be used as the constructor name.


A constructor declaration must always be part of a type declaration. It looks similar to a function declaration without a return type.

If a type has a constructor without arguments, dodo uses it to create the default value of the type when the module loads. If a type has no constructor, dodo uses the default value of its attributes to create the default value.

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.

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


class House
        House(Colour?, Integer?)

button RoundButton:
.                no default value

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.


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 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.


House(&Directory dir)

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.


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.



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, is more verbose 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, which can avoid programming errors.

^ 4.6. Constructors and Methods

v 4.6.2. Instance Methods