Dodo doc > Types > Automatic Type Conversion

Automatic Type Conversion

Dodo allows automatic type conversion from one type to another if the source type has a conversion to a type compatible with the target type. This conversion is provided by a conversion function.

Rationale: since dodo does not have type coercion, automatic type conversion is an important component of the type system. It is also useful for getting a return value from a method call or a message dispatch. Using a function for automatic type conversion makes it more flexible.


A conversion function is declared as a variable which type is prefixed with "=>". By default, accessing the variable throws a NoValueDefined exception.


=>Game played
=>Dessert asDessert
Rationale: even though a conversion function is really a function, it is used more like a variable which can be assigned a value, can be declared as a constant etc. The syntax attempts to convey this dual identity with an arrow in front of a variable declaration. Since a conversion function is used for type conversion, it makes more sense to throw an exception by default rather than returning the default value of the type.


A conversion function can be initialised with the name of a member function with no parameters (such as a constant, see section on constants).

You can override a conversion function with a normal function. This allows to write a body for the conversion function. When overriding a conversion function with a normal function, leave out the "=>" prefix for the return type.


=>String description() = defaultDescription   # constant
=>Game played = chooseGame # function

Dessert ^asDessert()
return cooked
Rationale: the initiation value of the conversion function should be member of the type so that it can access other members. Because the return value of the overriding function is not a conversion function, its type should not be prefixed with "=>" even though the name of the function is the same as the conversion function.


Automatic type conversion does not affect the compatibility between two types. Adding a conversion function has no effect on the compatibility of one type with another.

Rationale: the type which matters is the type which is really used not the type before conversion. Since automatic type conversion happens before passing the value to a function, the function in question does not need to be concerned with type compatibility of the value before conversion.

^ 4.8. Use of Variables as Type

v 5. Macros and Templates