choice.carbon 914 B

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