Skip to content

Support a very basic form of operator overloading #1520

Closed
@marijnh

Description

@marijnh

(RFC)

We could add interfaces core::num and core::deref, with the first implementing methods add, sub, mult, div, rem, neg, and probably a few more, and the second only deref. The compiler can then resolve operators +, -, *, /, %, and [x] on types that are not built-in numerics or vectors as if they were calls to the corresponding method (1 + 1 => 1.add(1)), and numeric-style and sequence-style user types can implement these interface to allow the operators to be applied to them.

This is not the most elegant solution (1 + 1 is more like add(1, 1) than 1.add(1)), but it would give us operator overloading on the short term, without first needing a lot of extra interface features.

We will need some kind of support for a 'self type' in interface declarations to be able to say that add takes an returns a value of the type that the interface was specialized on, which is not currently expressable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions