From: Brendan Hansen Date: Mon, 27 Feb 2023 00:17:29 +0000 (-0600) Subject: Merge branch 'result-type' X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=9acdcc9ab9ea22ec9e2c3e940ad725fa302965f4;p=onyx.git Merge branch 'result-type' --- 9acdcc9ab9ea22ec9e2c3e940ad725fa302965f4 diff --cc core/container/optional.onyx index e486e911,86f54d10..92a18794 --- a/core/container/optional.onyx +++ b/core/container/optional.onyx @@@ -83,13 -83,16 +83,23 @@@ Optional :: struct (Value_Type: type_ex return generate(); } + #doc """ + Returns the value inside the optional, if there is one. + If not, an assertion is thrown and the context's assert + handler must take care of it. + """ + unwrap :: (o: Optional) -> o.Value_Type { + if o.has_value do return o.value; + assert(false, "Unwrapping empty Optional."); + } + + or_return :: macro (o: Optional($T)) -> T { + value := o; + if value.has_value do return value.value; + + return #from_enclosing .{}; + } + hash :: (o: Optional($T/core.hash.Hashable)) -> u32 { if !o.has_value do return 0; return core.hash.to_u32(o.value);