Results.Macros: Macros

Exported members

Results.Macros.@catch_resultMacro

Catches an exception inside expr and returns a Result instead.

If a type is given, only exceptions of that type will be caught.

Examples

julia> @catch_result begin
           arr = [5,3,2]
           arr[4]
       end
Err(BoundsError([5, 3, 2], (4,)))
julia> @catch_result [5,3,2][3]
Ok(2)
julia> @catch_result TypeError [5,3,2][4]
ERROR: BoundsError: attempt to access 3-element Array{Int64,1} at index [4]
source
Results.Macros.@if_letMacro

Run then_block if the assignment expression returns an Ok or Some value. Runs else_block otherwise.

Example

julia> @if_let val = Some(5) begin
           2*val
       end begin
           0
       end
10
julia> @if_let val = Err("error") begin
           2*val
       end begin
           0
       end
0
julia> @if_let val = Err("error") begin
           println(val)
       end
source
Results.Macros.@some_ifMacro

If predicate, evaluate the enclosed expression wrapped in Some. Otherwise, return None.

Example

julia> try_get(a, index) = @some_if isassigned(a, index) a[index]
try_get (generic function with 1 method)
julia> try_get([2,3,4], 2)
Some(3)
julia> try_get([1,2,3], 10)
source
Results.Macros.@try_unwrapMacro

Unwraps an Ok or Some value, while returning error values upstream. Highly useful for chaining computations together.

Example

julia> function test(x::Result)::Result
           y = @try_unwrap(x) .- 5
           z = @try_unwrap try_pop!(y) |> ok_or("Empty array")
           Ok(z)
       end
test (generic function with 1 method)

julia> test(Ok([5, 8]))
Ok(3)
julia> test(Ok([]))
Err("Empty array")
julia> test(Err(5))
Err(5)
source
Results.Macros.@unwrap_orMacro

Macro version of unwrap_or, which allows for the embedding of control statements in the or clause.

Example

julia> for v in [[2,3,4], [3,4,5], [], [1]]
           println(@unwrap_or(try_get(v, 1), break))
       end
2
3
source
Results.Macros.@while_letMacro

Loop block while the assignment expression assign returns an Ok or Some value.

Example

julia> a = [1,2,3];

julia> @while_let val = try_pop!(a) begin
           print(val)
       end
321
source

Un-exported members