generic_choice_nested_in_template_class.carbon 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. // RUN: %{explorer} %s 2>&1 | \
  6. // RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes=false %s
  7. // RUN: %{explorer} --parser_debug --trace_file=- %s 2>&1 | \
  8. // RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes %s
  9. // AUTOUPDATE: %{explorer} %s
  10. // CHECK: H 22
  11. // CHECK: result: 0
  12. package ExplorerTest api;
  13. choice MyOptionalElement(T:! Type) {
  14. None(),
  15. Element(T)
  16. }
  17. class MyOptional(T:! Type){
  18. fn CreateEmpty() -> MyOptional(T){
  19. return { .element = MyOptionalElement(T).None() };
  20. }
  21. fn Create ( value: T ) -> MyOptional(T){
  22. return { .element = MyOptionalElement(T).Element(value) };
  23. }
  24. fn has_value[me: Self] () -> bool{
  25. var x: MyOptionalElement(T) = me.element;
  26. match(x){
  27. case MyOptionalElement(T).None() => { return false; }
  28. }
  29. return false;
  30. }
  31. fn get[me: Self] () -> T {
  32. var y: T;
  33. var x: MyOptionalElement(T) = me.element;
  34. match(x){
  35. case MyOptionalElement(T).Element( var x: T ) =>{
  36. return x;
  37. }
  38. }
  39. return y;
  40. }
  41. var element: MyOptionalElement(T);
  42. }
  43. fn Main() -> i32 {
  44. var f: MyOptional(i32) = MyOptional(i32).Create(22);
  45. var x: i32 = f.get();
  46. Print("H {0}",x);
  47. return 0;
  48. }