param_impl2.carbon 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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: result: 0
  7. package ExplorerTest api;
  8. interface Number {
  9. fn Zero() -> Self;
  10. fn Add[self: Self](other: Self) -> Self;
  11. }
  12. class Point(T:! Number) {
  13. var x: T;
  14. var y: T;
  15. }
  16. external impl i32 as Number {
  17. fn Zero() -> i32 { return 0; }
  18. fn Add[self: i32](other: i32) -> i32 { return self + other; }
  19. }
  20. external impl forall [U:! Number] Point(U) as Number {
  21. fn Zero() -> Point(U) { return {.x = U.Zero(), .y = U.Zero() }; }
  22. fn Add[self: Point(U)](other: Point(U)) -> Point(U) {
  23. return {.x = self.x.Add(other.x), .y = self.y.Add(other.y)};
  24. }
  25. }
  26. fn Sum[E:! Number](x: E, y: E) -> E {
  27. var total: E = E.Zero();
  28. total = total.Add(x);
  29. total = total.Add(y);
  30. return total;
  31. }
  32. fn SumPoints[E:! Number](p: Point(E), q: Point(E)) -> Point(E) {
  33. return Sum(p, q);
  34. }
  35. fn Main() -> i32 {
  36. var p: Point(i32) = {.x = 1, .y = 2};
  37. var q: Point(i32) = {.x = 4, .y = 3};
  38. var r: Point(i32) = SumPoints(p, q);
  39. return r.x - r.y;
  40. }