generic_class_substitution.carbon 1.0 KB

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