| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
- // Exceptions. See /LICENSE for license information.
- // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- //
- // AUTOUPDATE
- // CHECK:STDOUT: H 22
- // CHECK:STDOUT: result: 0
- package ExplorerTest api;
- choice MyOptionalElement(T:! type) {
- None(),
- Element(T)
- }
- class MyOptional(T:! type){
- fn CreateEmpty() -> MyOptional(T){
- return { .element = MyOptionalElement(T).None() };
- }
- fn Create ( value: T ) -> MyOptional(T){
- return { .element = MyOptionalElement(T).Element(value) };
- }
- fn has_value[self: Self] () -> bool{
- var x: MyOptionalElement(T) = self.element;
- match(x){
- case MyOptionalElement(T).None() => { return false; }
- }
- return false;
- }
- fn get[self: Self] () -> T {
- var y: T;
- var x: MyOptionalElement(T) = self.element;
- match(x){
- case MyOptionalElement(T).Element( var x: T ) =>{
- return x;
- }
- }
- return y;
- }
- var element: MyOptionalElement(T);
- }
- fn Main() -> i32 {
- var f: MyOptional(i32) = MyOptional(i32).Create(22);
- var x: i32 = f.get();
- Print("H {0}",x);
- return 0;
- }
|