Python-flavored Option[T]
and Result[T, E]
, empowered with static typing.
Option[T]
=Some[T]
|Null
- support for
match
statement:match option: case Some(value): print(value) case Null.null: ...
Result[T, E]
=Ok[T]
|Err[E]
- support for
match
statement:match result: case Ok(value): print(value) case Err(err): print(err)
from_none
turnsT | None
intoOption[T]
:data: dict[str, int] = {} value: Option[int] = from_none(data.get("foo"))
try_option
turns functions thatreturn T
orraise E
intoOption[T]
try_result
turns functions thatreturn T
orraise E
intoResult[T, E]
CatchResult
captures the result (or exception) of a block of code intoResult
:with CatchResult(OSError) as catch: catch @= await client.get("foo") catch.result # Ok(data) | Err(OSError(...))
collect_options
collectsIterable[Option[T]]
intoSome[list[T]]
iff all options areSome
, elseNull
collect_results
collectsIterable[Result[T, E]]
intoOk[list[T]]
iff all results areOk
, else the firstErr[E]