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

Reimplement operator support through operator traits #6

Open
kodek16 opened this issue May 15, 2020 · 0 comments
Open

Reimplement operator support through operator traits #6

kodek16 opened this issue May 15, 2020 · 0 comments

Comments

@kodek16
Copy link
Owner

kodek16 commented May 15, 2020

Add a set of traits corresponding to various operators in the language (e.g. Add, Sub, Index), and rewrite operator support in the compiler to delegate to methods of these traits. This would also allow users to implement operator support for custom types by implementing the operator traits.

The exact design of these traits depends on how powerful the trait system is by that point, but here is an idea of how they could look like:

trait Add<T> {
  fun add(self, rhs: T): T;
}

struct MyNumber : Add<MyNumber> {
  // ...

  fun add(self, rhs: MyNumber): MyNumber {
    // ...
  }
}

fun main() {
  var x: MyNumber, y: MyNumber;

  // Expands to x.add(y).
  var z = x + y;

  // Causes an error that clearly states that `bool` does not implement `Add<bool>`.
  var b = true + false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant