// Look to std/prelude.moc for `dup` and `drop` implementations // Note: MyResult is defined in the stdlib, so we use MyMyResult... here. enum MyResultTag { Ok Err } union MyResultValue { T: ok E: err } struct MyResult { MyResultValue: value MyResultTag : tag impl: fn MyResult.Ok(T: value) -> [MyResult] { value cast(MyResultValue) MyResultTag::Ok cast(MyResult) } fn MyResult.Err(E) -> [MyResult] { cast(MyResultValue) MyResultTag::Err cast(MyResult) } fn MyResult.is_ok(MyResult: r) -> [bool] { r::tag MyResultTag::Ok == } fn MyResult.is_err(MyResult: r) -> [bool] { r::tag MyResultTag::Err == } fn MyResult.unwrap(MyResult: r) -> [T] { r MyResult.is_err if { "Tried to unrwap a MyResult in an Error state. Exiting program" println 1 exit } r::value::ok } fn MyResult.unwrap_err(MyResult: r) -> [E] { r MyResult.is_ok if { "Tried to unrwap_err a MyResult in an Ok state. Exiting program" println 1 exit } r::value::err } } fn might_fail(u64: n) -> [MyResult] { n 2 % 0 == if { "Woah didn't expect to see an even number here" MyResult.Err:: } else { n 2 * 1 + MyResult.Ok:: } } fn main() { 12345 // uncomment this to see the compiler error // MyResult.Ok MyResult.Ok:: as [ok] // Notice that you can put any type here! "Uh Oh! Something went wrong!" MyResult.Err:: as [err] "ok is in ok state: " print ok MyResult.is_ok println "ok is in err state: " print ok MyResult.is_err println "err is in ok state: " print err MyResult.is_ok println "err is in err state: " print err MyResult.is_err println ok MyResult.is_ok if { "unwrapped ok value: " print ok MyResult.unwrap println } err MyResult.is_err if { "Unwrapped err value: " print err MyResult.unwrap_err println } 0 while dup 10 < do { as [i] i might_fail as [r] { r MyResult.is_ok if { "Didn't fail: " print r MyResult.unwrap println } else { "Did fail: " print r MyResult.unwrap_err println } } i 1 + } drop }