Add extensibility support for arithmetic operators. (#1751)

Add new prelude interfaces `Negate`, `AddWith`, `SubWith`, and `MulWith`, and
use them to implement non-builtin arithmetic operations.
This commit is contained in:
Richard Smith
2022-07-27 15:09:27 -07:00
committed by GitHub
parent 34a18b5e9e
commit 15ec5113aa
20 changed files with 401 additions and 57 deletions
@@ -11,7 +11,7 @@
package ExplorerTest api;
interface AddWith(T:! Type) {
interface MyAddWith(T:! Type) {
fn Op[me: Self](b: T) -> Self;
}
@@ -21,13 +21,13 @@ class Point {
}
// Allowed: `Self` means `Point` after `as`
impl Point as AddWith(Self) {
impl Point as MyAddWith(Self) {
fn Op[me: Point](b: Point) -> Point {
return {.x = me.x + b.x, .y = me.y + b.y};
}
}
fn DoAddGeneric[T:! Type, U:! AddWith(T)](a: U, b: T) -> U {
fn DoAddGeneric[T:! Type, U:! MyAddWith(T)](a: U, b: T) -> U {
return a.Op(b);
}