comparison.carbon 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. package Core library "prelude/operators/comparison";
  5. export import library "prelude/types/bool";
  6. import library "prelude/types/int_literal";
  7. // TODO: Per the design, for each *With interface there should also be a
  8. // non-With named constraint, such as:
  9. //
  10. // constraint Eq {
  11. // extend require impls EqWith(Self);
  12. // }
  13. // Equality comparison: `a == b` and `a != b`.
  14. interface EqWith(Other:! type) {
  15. fn Equal[self: Self](other: Other) -> bool;
  16. fn NotEqual[self: Self](other: Other) -> bool;
  17. }
  18. // Relational comparison: `a < b`, `a <= b`, `a > b`, `a >= b`.
  19. interface OrderedWith(Other:! type) {
  20. // TODO: fn Compare
  21. fn Less[self: Self](other: Other) -> bool;
  22. fn LessOrEquivalent[self: Self](other: Other) -> bool;
  23. fn Greater[self: Self](other: Other) -> bool;
  24. fn GreaterOrEquivalent[self: Self](other: Other) -> bool;
  25. }
  26. // Equality comparison for `bool`.
  27. // Note that this must be provided in this library as `bool` doesn't have any
  28. // associated libraries of its own.
  29. impl bool as EqWith(Self) {
  30. fn Equal[self: Self](other: Self) -> bool = "bool.eq";
  31. fn NotEqual[self: Self](other: Self) -> bool = "bool.neq";
  32. }
  33. // Operations for IntLiteral. These need to be here because IntLiteral has no
  34. // associated library of its own.
  35. impl IntLiteral() as EqWith(Self) {
  36. fn Equal[self: Self](other: Self) -> bool = "int.eq";
  37. fn NotEqual[self: Self](other: Self) -> bool = "int.neq";
  38. }
  39. impl IntLiteral() as OrderedWith(Self) {
  40. // TODO: fn Compare
  41. fn Less[self: Self](other: Self) -> bool = "int.less";
  42. fn LessOrEquivalent[self: Self](other: Self) -> bool = "int.less_eq";
  43. fn Greater[self: Self](other: Self) -> bool = "int.greater";
  44. fn GreaterOrEquivalent[self: Self](other: Self) -> bool = "int.greater_eq";
  45. }