class_function.carbon 874 B

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. // AUTOUPDATE
  6. // RUN: %{explorer-run}
  7. // RUN: %{explorer-run-trace}
  8. // CHECK:STDOUT: result: 0
  9. package ExplorerTest api;
  10. interface Number {
  11. fn Zero() -> Self;
  12. fn Add[me: Self](other: Self) -> Self;
  13. }
  14. class Point(T:! Number) {
  15. fn Origin() -> Point(T) {
  16. return {.x = T.Zero(), .y = T.Zero()};
  17. }
  18. fn SumXY(p: Point(T)) -> T {
  19. return p.x.Add(p.y);
  20. }
  21. fn SumFn() -> (__Fn(Point(T)) -> T) {
  22. return Point(T).SumXY;
  23. }
  24. var x: T;
  25. var y: T;
  26. }
  27. external impl i32 as Number {
  28. fn Zero() -> i32 { return 0; }
  29. fn Add[me: i32](other: i32) -> i32 { return me + other; }
  30. }
  31. fn Main() -> i32 {
  32. var p: Point(i32) = Point(i32).Origin();
  33. return p.SumFn()(p);
  34. }