choice.carbon 882 B

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