We should probably think about our story for this at some point.
We could add generics/templates and discriminated unions and go for a Result type, but that's... a lot.
We could do multiple return. Maybe say that you return your error first, then your value, and provide a syntax...
fun foo(a: u8) -> err, u8
-- do something
endfun
fun bar() -> err, u8
let x = try foo(7) -- causes early-return on error
return x + 7 -- returns ok as the error
endfun
-- bar desugars to this, where e is some kind of gensym
fun bar() -> err, u8
let e, x = foo(7)
if e != ok do return e end
return ok, x + 7
end
EDIT: Of course, err is a new type... it's one byte wide, has the special value literal ok, and allows 1-255 as literals. You can't do arithmetic with it or assign u8s to it. If a conditional expression has type err, ok is considered truthy.
We should probably think about our story for this at some point.
We could add generics/templates and discriminated unions and go for a Result type, but that's... a lot.
We could do multiple return. Maybe say that you return your error first, then your value, and provide a syntax...
EDIT: Of course,
erris a new type... it's one byte wide, has the special value literalok, and allows 1-255 as literals. You can't do arithmetic with it or assignu8s to it. If a conditional expression has typeerr,okis considered truthy.