class_function.carbon 818 B

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