custom_equality.carbon 859 B

123456789101112131415161718192021222324252627282930313233
  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. // RUN: %{explorer-run}
  7. // RUN: %{explorer-run-trace}
  8. // CHECK:STDOUT: structs equal: 0
  9. // CHECK:STDOUT: structs not equal: 1
  10. // CHECK:STDOUT: result: 0
  11. package ExplorerTest api;
  12. class MyType {
  13. var value: i32;
  14. impl as EqWith(Self) {
  15. fn Equal[self: Self](other: Self) -> bool {
  16. return self.value == other.value;
  17. }
  18. fn NotEqual[self: Self](other: Self) -> bool{
  19. return self.value != other.value;
  20. }
  21. }
  22. }
  23. fn Main() -> i32 {
  24. let x: MyType = {.value = 1};
  25. let y: MyType = {.value = 2};
  26. Print("structs equal: {0}", if x == y then 1 else 0);
  27. Print("structs not equal: {0}", if x != y then 1 else 0);
  28. return 0;
  29. }