generic_choice_nested_in_template_class.carbon 1.2 KB

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