choice.carbon 917 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. choice Ints {
  8. None,
  9. One(i32),
  10. Two(i32, i32)
  11. }
  12. // Test some alternate syntaxes
  13. choice MoreInts {
  14. None(),
  15. One(i32),
  16. Two(i32, i32),
  17. }
  18. fn Main() -> i32 {
  19. var x: Ints = Ints.None;
  20. var y: Ints = Ints.One(42);
  21. var z: MoreInts = MoreInts.None();
  22. var n: i32 = 0;
  23. match (y) {
  24. case Ints.None => { n = n + 2; }
  25. case Ints.One(x: auto) => { n = x + 1 - 42; }
  26. case Ints.Two(a: auto, b: auto) => { n = 2; }
  27. }
  28. match (x) {
  29. case Ints.One(x: auto) => { n = x + 2; }
  30. case Ints.None => { n = n - 1; }
  31. case Ints.Two(x: auto, y: auto) => { n = 5; }
  32. }
  33. match (z) {
  34. case MoreInts.None() => { ++n; }
  35. }
  36. return n;
  37. }
  38. // CHECK:STDOUT: result: 1