mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 22:02:52 +01:00
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:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user