Dodo doc > Types > 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.
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.
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")
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)
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.