generic_class_substitution.carbon 756 B

123456789101112131415161718192021222324252627282930313233343536
  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. package ExplorerTest api;
  7. interface Number {
  8. fn Zero() -> Self;
  9. fn Add[self: Self](other: Self) -> Self;
  10. }
  11. class Point(T:! Number) {
  12. fn Origin() -> Point(T) {
  13. return {.x = T.Zero(), .y = T.Zero()};
  14. }
  15. var x: T;
  16. var y: T;
  17. }
  18. impl i32 as Number {
  19. fn Zero() -> i32 { return 0; }
  20. fn Add[self: i32](other: i32) -> i32 { return self + other; }
  21. }
  22. fn SumXY[U:! Number](p: Point(U)) -> U {
  23. return p.Origin().x.Add(p.y);
  24. }
  25. fn Main() -> i32 {
  26. var p: Point(i32) = {.x = 0, .y = 0};
  27. return SumXY(p);
  28. }
  29. // CHECK:STDOUT: result: 0