choice1.carbon 804 B

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