generic_class_substitution.carbon 818 B

12345678910111213141516171819202122232425262728293031323334353637
  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. fn Origin() -> Point(T) {
  16. return {.x = T.Zero(), .y = T.Zero()};
  17. }
  18. var x: T;
  19. var y: T;
  20. }
  21. external impl i32 as Number {
  22. fn Zero() -> i32 { return 0; }
  23. fn Add[self: i32](other: i32) -> i32 { return self + other; }
  24. }
  25. fn SumXY[U:! Number](p: Point(U)) -> U {
  26. return p.Origin().x.Add(p.y);
  27. }
  28. fn Main() -> i32 {
  29. var p: Point(i32) = {.x = 0, .y = 0};
  30. return SumXY(p);
  31. }