Dodo doc > Types > Declaration



Declaration

Declaration with initialisation value

The simplest way to declare a type is with an initialisation value. In this form, the declaration starts with "def" followed by the type name, then "=" and the initialisation value. The expression after "=" is assigned to the new type and should evaluate to a type.

Examples:

def SimpleStr = String
def ItemType = Object

Note: A type can be publicly declared with an initialisation value but have a different implementation in the private section. In that case the private declaration should conform to the type of the initialisation value.

Rationale: this syntax is consistent with the way a variable is declared which helps keeping things simple. The dodo language does not define a type for types, so this form uses "def" instead. The provision that the type can be implemented independently from the public initialisation value allows to reuse just the interface of a type.

Declaration with prototype

The first element of the declaration is the name of a variable to use as prototype, or "def" to use the default prototype (struct). This prototype is used as base for all instances of the type via the default value. The new type inherits attributes and functions from the prototype type.

The second element of the declaration is a valid type name for the type.

The type name can be followed by a list of qualifiers which apply to the type, each preceded with "is". Qualifiers are declared separately (see the section on qualifiers).

Finally the declaration can end with a declaration block which is the body of the type. Attributes and constructors should be declared in this block. It can also contain any other kind of declaration and definition.

Constructors are like functions which name is the name of the type.

Examples:

class House is Drawable, is Colourful
{
Colour wallColour
Integer windowCount
House(Colour?, Integer?)
}

apple ShinyApple:
def shine() = "ziing!".

If the prototype is replaced with "def" then the default prototype is used, which is the same as "struct".

Note: The prototype declared publicly can differ from the prototype in the private section, as long as the private prototype is compatible with the public one. Added attributes in the private prototype become private attributes of the type.

Rationale: when the prototype is struct or class, this form has similar syntax to C# or C++. The use of a prototype which can be private allows types to reuse an existing implementation without necessarily exposing the associated interface.


^ 4.1. Syntax

v 4.3. Use as Prototype