param_impl.carbon 1.1 KB

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