method.carbon 626 B

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