generic_fun_and_class.carbon 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. // RUN: %{executable_semantics} %s 2>&1 | \
  6. // RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes=false %s
  7. // RUN: %{executable_semantics} --trace %s 2>&1 | \
  8. // RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes %s
  9. // AUTOUPDATE: %{executable_semantics} %s
  10. // CHECK: result: 0
  11. package ExecutableSemanticsTest api;
  12. interface Number {
  13. fn Zero() -> Self;
  14. fn Add[me: Self](other: Self) -> Self;
  15. }
  16. class Point(T:! Number) {
  17. var x: T;
  18. var y: T;
  19. }
  20. fn Origin[U :! Number](other: U) -> Point(U) {
  21. return {.x = U.Zero(), .y = U.Zero()};
  22. }
  23. fn Clone[U :! Number](other: Point(U)) -> Point(U) {
  24. return {.x = other.x, .y = other.y};
  25. }
  26. fn SumXY[U :! Number](other: Point(U)) -> U {
  27. return other.x.Add(other.y);
  28. }
  29. external impl i32 as Number {
  30. fn Zero() -> i32 { return 0; }
  31. fn Add[me: i32](other: i32) -> i32 { return me + other; }
  32. }
  33. fn Main() -> i32 {
  34. var p: Point(i32) = Origin(0);
  35. return SumXY(Clone(p));
  36. }