Implement == and != for bool. (#2182)

This commit is contained in:
Richard Smith
2022-09-14 21:55:39 -07:00
committed by GitHub
parent 81cdbfe650
commit bfcab23795
2 changed files with 61 additions and 16 deletions
+17 -8
View File
@@ -83,6 +83,15 @@ impl forall [T2:! Type, U2:! Type, T1:! EqWith(T2), U1:! EqWith(U2)]
}
}
impl bool as EqWith(Self) {
fn Equal[me: Self](other: Self) -> bool {
return if me then other else not other;
}
fn NotEqual[me: Self](other: Self) -> bool {
return if me then not other else other;
}
}
impl i32 as EqWith(Self) {
fn Equal[me: Self](other: Self) -> bool {
return __intrinsic_int_eq(me, other);
@@ -437,16 +446,16 @@ choice OptionalElement(T:! Type) {
Element(T)
}
class Optional(T:! Type){
fn CreateEmpty() -> Optional(T){
return { .element = OptionalElement(T).None() };
class Optional(T:! Type) {
fn CreateEmpty() -> Optional(T) {
return {.element = OptionalElement(T).None()};
}
fn Create(value: T ) -> Optional(T){
return { .element = OptionalElement(T).Element(value) };
fn Create(value: T) -> Optional(T) {
return {.element = OptionalElement(T).Element(value)};
}
fn HasValue[me: Self]() -> bool {
match(me.element){
match(me.element) {
case OptionalElement(T).None() => { return false; }
}
return true;
@@ -454,8 +463,8 @@ class Optional(T:! Type){
fn Get[me: Self]() -> T {
var y: T;
match(me.element){
case OptionalElement(T).Element(x: T ) =>{
match(me.element) {
case OptionalElement(T).Element(x: T) => {
return x;
}
}
+44 -8
View File
@@ -7,18 +7,54 @@
// RUN: %{explorer} --parser_debug --trace_file=- %s 2>&1 | \
// RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes %s
// AUTOUPDATE: %{explorer} %s
// CHECK: strings equal: 0
// CHECK: ints equal: 1
// CHECK: strings not eq: 1
// CHECK: ints not eq: 0
// CHECK: bool: 1
// CHECK: bool: 1
// CHECK: bool: 1
// CHECK: bool: 1
// CHECK: bool: 1
// CHECK: bool: 1
// CHECK: bool: 1
// CHECK: bool: 1
// CHECK: string: 1
// CHECK: string: 1
// CHECK: string: 1
// CHECK: string: 1
// CHECK: string: 1
// CHECK: string: 1
// CHECK: string: 1
// CHECK: string: 1
// CHECK: int: 1
// CHECK: int: 1
// CHECK: int: 1
// CHECK: int: 1
// CHECK: int: 1
// CHECK: int: 1
// CHECK: int: 1
// CHECK: int: 1
// CHECK: result: 0
package ExplorerTest api;
fn CompareEqualValues[T:! EqWith(.Self)](format: String, a: T, b: T) {
Print(format, if a == b then 1 else 0);
Print(format, if a != b then 0 else 1);
}
fn CompareDifferentValues[T:! EqWith(.Self)](format: String, a: T, b: T) {
Print(format, if a == b then 0 else 1);
Print(format, if a != b then 1 else 0);
}
fn CompareAll[T:! EqWith(.Self)](format: String, a: T, b: T) {
CompareEqualValues(format, a, a);
CompareEqualValues(format, b, b);
CompareDifferentValues(format, a, b);
CompareDifferentValues(format, b, a);
}
fn Main() -> i32 {
Print("strings equal: {0}", if "hello" == "world" then 1 else 0);
Print("ints equal: {0}", if 1 == 1 then 1 else 0);
Print("strings not eq: {0}", if "hello" != "world" then 1 else 0);
Print("ints not eq: {0}", if 1 != 1 then 1 else 0);
CompareAll("bool: {0}", false, true);
CompareAll("string: {0}", "hello", "world");
CompareAll("int: {0}", 1, 2);
return 0;
}