generic_fun_and_class.carbon 879 B

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