use_self.carbon 623 B

1234567891011121314151617181920212223242526272829
  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. class Point(T:! type) {
  11. // Allowed: `Self` means `Point(T)` here.
  12. fn Origin(zero: T) -> Self {
  13. return {.x = zero, .y = zero};
  14. }
  15. fn GetX[self: Self]() -> T {
  16. return self.x;
  17. }
  18. var x: T;
  19. var y: T;
  20. }
  21. fn Main() -> i32 {
  22. var p: Point(i32) = Point(i32).Origin(0);
  23. return p.GetX();
  24. }