inc_dec.carbon 891 B

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