generic_class_substitution.carbon 764 B

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