Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ad hoc type design #236

Closed
bvssvni opened this issue May 27, 2016 · 0 comments
Closed

Ad hoc type design #236

bvssvni opened this issue May 27, 2016 · 0 comments

Comments

@bvssvni
Copy link
Member

bvssvni commented May 27, 2016

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant