inc_dec.carbon 906 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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: 6
  7. // CHECK:STDOUT: 5
  8. // CHECK:STDOUT: 6
  9. // CHECK:STDOUT: 5
  10. // CHECK:STDOUT: 6
  11. // CHECK:STDOUT: 5
  12. // CHECK:STDOUT: 6
  13. // CHECK:STDOUT: 5
  14. // CHECK:STDOUT: result: 0
  15. package ExplorerTest api;
  16. class A { var n: i32; }
  17. external impl A as Inc {
  18. fn Op[addr self: Self*]() { ++self->n; }
  19. }
  20. external impl A as Dec {
  21. fn Op[addr self: Self*]() { --self->n; }
  22. }
  23. fn Main() -> i32 {
  24. var a: A = {.n = 5};
  25. ++a.n;
  26. Print("{0}", a.n);
  27. --a.n;
  28. Print("{0}", a.n);
  29. ++a;
  30. Print("{0}", a.n);
  31. --a;
  32. Print("{0}", a.n);
  33. a.n.(Inc.Op)();
  34. Print("{0}", a.n);
  35. a.n.(Dec.Op)();
  36. Print("{0}", a.n);
  37. a.(Inc.Op)();
  38. Print("{0}", a.n);
  39. a.(Dec.Op)();
  40. Print("{0}", a.n);
  41. return 0;
  42. }