generic_fun_and_class.carbon 871 B

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