point_with_interface.carbon 1.1 KB

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. // RUN: %{explorer} %s 2>&1 | \
  6. // RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes=false %s
  7. // RUN: %{explorer} --parser_debug --trace_file=- %s 2>&1 | \
  8. // RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes %s
  9. // AUTOUPDATE: %{explorer} %s
  10. // CHECK: result: 0
  11. package ExplorerTest 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. fn Clone[me: Point(T)]() -> Point(T) {
  21. return {.x = me.x, .y = me.y};
  22. }
  23. fn SumXY[me: Point(T)]() -> T {
  24. return me.x.Add(me.y);
  25. }
  26. var x: T;
  27. var y: T;
  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) = Point(i32).Origin();
  35. return p.Clone().SumXY();
  36. }