-
-
Notifications
You must be signed in to change notification settings - Fork 55
Description
Dyon supports types that are ad-hoc, or partially defined. An ad-hoc type can have any name not used by other Dyon types. It is common to use CamelCase.
An ad-hoc type has an optional inner type where you put more details about the type. When not specified, this is set to {}
. For example Person
and Person {}
is the same type.
An ad-hoc type is not declared, but written directly in the function declaration.
Example:
fn new_person(name: str) -> Person {
return { name: name }
}
The inner type goes with the ad-hoc type and vice versa. An error is shown if you try to use a different ad-hoc type or a different inner type where an ad-hoc type is expected.
Addition and multiplication
Two ad-hoc types can be added if the inner type allows addition.
Dyon complains if you try to add an ad-hoc type with an inner type.
For example:
// Convert number to `km`
fn km(v: f64) -> km f64 { return v }
km(3) + km(4) // OK
km(3) + 4 // ERROR
Before #638, multiplication is not allowed, because this often changes physical units.
After #638, multiplication is allowed, because one can override the multiplication operator to deal with physical units.