generic_choice_nested_in_template_class.carbon 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. package ExplorerTest api;
  7. choice MyOptionalElement(T:! type) {
  8. None(),
  9. Element(T)
  10. }
  11. class MyOptional(T:! type){
  12. fn CreateEmpty() -> MyOptional(T){
  13. return { .element = MyOptionalElement(T).None() };
  14. }
  15. fn Create ( value: T ) -> MyOptional(T){
  16. return { .element = MyOptionalElement(T).Element(value) };
  17. }
  18. fn has_value[self: Self] () -> bool{
  19. var x: MyOptionalElement(T) = self.element;
  20. match(x){
  21. case MyOptionalElement(T).None() => { return false; }
  22. }
  23. return false;
  24. }
  25. fn get[self: Self] () -> T {
  26. var x: MyOptionalElement(T) = self.element;
  27. match(x){
  28. case MyOptionalElement(T).Element( var x: T ) =>{
  29. return x;
  30. }
  31. }
  32. // TODO: Mark this as unreachable somehow.
  33. return get();
  34. }
  35. var element: MyOptionalElement(T);
  36. }
  37. fn Main() -> i32 {
  38. var f: MyOptional(i32) = MyOptional(i32).Create(22);
  39. var x: i32 = f.get();
  40. Print("H {0}",x);
  41. return 0;
  42. }
  43. // CHECK:STDOUT: H 22
  44. // CHECK:STDOUT: result: 0