class_function.carbon 817 B

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