Skip to content

Ad hoc type design #236

@bvssvni

Description

@bvssvni

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions