mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Support for user-defined Eq impls (#1730)
Related to #1549; user-defined equality is a prerequisite for user-defined comparison.
This commit is contained in:
-3
@@ -14,9 +14,6 @@ package ExplorerTest api;
|
||||
fn Main() -> i32 {
|
||||
var empty: {} = {};
|
||||
empty = {};
|
||||
if (not (empty == {})) {
|
||||
return 1;
|
||||
}
|
||||
match (empty) {
|
||||
case {} => {
|
||||
return 0;
|
||||
|
||||
+7
@@ -11,6 +11,13 @@
|
||||
|
||||
package ExplorerTest api;
|
||||
|
||||
// TODO: Implement this with some kind of reflection?
|
||||
external impl {.x: i32, .y: i32} as EqWith(Self) {
|
||||
fn Equal[me: Self](other: Self) -> Bool {
|
||||
return me.x == other.x and me.y == other.y;
|
||||
}
|
||||
}
|
||||
|
||||
fn Main() -> i32 {
|
||||
var t1: {.x: i32, .y: i32} = {.x = 5, .y = 2};
|
||||
var t2: {.x: i32, .y: i32} = {.x = 5, .y = 2};
|
||||
|
||||
@@ -11,6 +11,13 @@
|
||||
|
||||
package ExplorerTest api;
|
||||
|
||||
// TODO: Implement this with some kind of reflection?
|
||||
external impl {.x: i32, .y: i32} as EqWith(Self) {
|
||||
fn Equal[me: Self](other: Self) -> Bool {
|
||||
return me.x == other.x and me.y == other.y;
|
||||
}
|
||||
}
|
||||
|
||||
fn Main() -> i32 {
|
||||
var t1: {.x: i32, .y: i32} = {.x = 5, .y = 2};
|
||||
var t2: {.x: i32, .y: i32} = {.x = 5, .y = 4};
|
||||
|
||||
+8
-3
@@ -7,12 +7,17 @@
|
||||
|
||||
package ExplorerTest api;
|
||||
|
||||
// TODO: Implement this with some kind of reflection?
|
||||
external impl {.x: i32, .y: i32} as EqWith(Self) {
|
||||
fn Equal[me: Self](other: Self) -> Bool {
|
||||
return me.x == other.x and me.y == other.y;
|
||||
}
|
||||
}
|
||||
|
||||
fn Main() -> i32 {
|
||||
var t1: {.x: i32, .y: i32} = {.x = 5, .y = 2};
|
||||
var t2: {.x: i32,} = {.x = 5,};
|
||||
// CHECK: COMPILATION ERROR: {{.*}}/explorer/testdata/struct/fail_equality_type.carbon:[[@LINE+3]]: type error in ==
|
||||
// CHECK: expected: {.x: i32, .y: i32}
|
||||
// CHECK: actual: {.x: i32}
|
||||
// CHECK: COMPILATION ERROR: {{.*}}/explorer/testdata/struct/fail_equality_type.carbon:[[@LINE+1]]: {.x: i32, .y: i32} is not equality comparable with {.x: i32} (could not find implementation of interface EqWith(U = {.x: i32}) for {.x: i32, .y: i32})
|
||||
if (t1 == t2) {
|
||||
return 1;
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user