custom_equality.carbon 805 B

12345678910111213141516171819202122232425262728293031
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // AUTOUPDATE
  6. // CHECK:STDOUT: structs equal: 0
  7. // CHECK:STDOUT: structs not equal: 1
  8. // CHECK:STDOUT: result: 0
  9. package ExplorerTest api;
  10. class MyType {
  11. var value: i32;
  12. impl as EqWith(Self) {
  13. fn Equal[self: Self](other: Self) -> bool {
  14. return self.value == other.value;
  15. }
  16. fn NotEqual[self: Self](other: Self) -> bool{
  17. return self.value != other.value;
  18. }
  19. }
  20. }
  21. fn Main() -> i32 {
  22. let x: MyType = {.value = 1};
  23. let y: MyType = {.value = 2};
  24. Print("structs equal: {0}", if x == y then 1 else 0);
  25. Print("structs not equal: {0}", if x != y then 1 else 0);
  26. return 0;
  27. }