generic_choice_nested_in_template_class.carbon 1.3 KB

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