choice1.carbon 766 B

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