method.carbon 623 B

123456789101112131415161718192021222324252627282930313233
  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. class Point {
  9. fn Origin() -> Point {
  10. return {.x = 0, .y = 0};
  11. }
  12. fn GetSetX[addr self: Point*](x: i32) -> i32 {
  13. var old: auto = (*self).x;
  14. (*self).x = x;
  15. return old;
  16. }
  17. var x: i32;
  18. var y: i32;
  19. }
  20. fn Main() -> i32 {
  21. var p: Point = Point.Origin();
  22. var x: auto = p.GetSetX(42);
  23. if (p.x == 42) {
  24. return x;
  25. }
  26. return 1;
  27. }