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.

Declaration

A conversion function is declared as a function without parameters where the arrow "->" before the return type is replaced with "=>". By default, calling the function throws a NoValueDefined exception.

Examples:

def description => String
def played => Game
def House.asDrawing => Drawing
def asDessert => Dessert

If the conversion function is the same for all members of the type, it can be declared as a type member.

Rationale: A conversion function is called automatically to convert the type of the value so it has no parameters. The double arrow emphasises the importance of the return type. 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. Declaring the conversion function as type member means that it does not need to be stored individually in each instance of the type.

Definition

A conversion function can be initialised with the name of a member function with no parameters (such as a constant, see section on constants). If the conversion function is a type member then it should be initialised with a function that takes one instance of the type as argument.

Define a conversion function like a constant or like a normal function where "->" is replaced with "=>" before the return continuation. If a return continuation is specified then the brackets are optional after the function name since the argument list is empty. If the conversion function is a type member then the function has one argument of the containing type.

Examples:

def description() = defaultDescription
def played = chooseGame     # assign a function to played
def House.asDrawing(House house) = house.draw
def asDessert => return(Dessert)
{
return cooked
}

To give the conversion function a new value in a constructor or in a method, use the following syntax:

.description => set("This is a new description")
Rationale: the double arrow mirrors the conversion function declaration syntax. Because it is a function and not a variable, the conversion function cannot be assigned a value directly. The latter syntax is a shorthand form to assign an anonymous function without arguments. A type member cannot be modified in a method or a constructor so there is no special syntax for it.
For a regular conversion function the initiation value should be member of the type so that it can access other members.

Use

Just use the variable of one type as if it was the other type and dodo will call the appropriate conversion function.

Example:

def dish = processor.Mix(ingredients, 10)
TasteDessert(dish)   # Tasting the cooked dessert

The conversion function can also be called explicitly.

Examples:

dish.asDessert
House.asDrawing(prettyHouse)
Rationale: automatic type conversion is the purpose of a conversion function. Since it is still a function, it is always possible to call it explicitly which can be useful at times.

Compatibility

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