Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Try.unwrap_err
Try.oktype
Try.errtype
Try.map
Try.astuple
```

## Short-circuit evaluation
Expand Down
1 change: 1 addition & 0 deletions src/Try.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct IsOkError <: InternalPrelude.Exception
end

function map end
function astuple end

macro and_return end
function var"@?" end
Expand Down
9 changes: 9 additions & 0 deletions src/branch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,15 @@ macro or(ex, rest...)
end
end

function Try.astuple(result)
br = branch(result)
if br isa Break
()
else
(valueof(br),)
end
end

###
### Currying
###
Expand Down
25 changes: 25 additions & 0 deletions src/docs/astuple.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Try.astuple(result) -> (value,) or ()
Try.astuple(Ok(value)) -> (value,)
Try.astuple(::Err) -> ()
Try.astuple(Some(value)) -> (value,)
Try.astuple(nothing) -> ()

Return a singleton tuple with the value if the result is "successful"; return an empty tuple
otherwise.

# Examples
```julia
julia> using Try

julia> Try.astuple(Ok(1))
(1,)

julia> Try.astuple(Err(1))
()

julia> Try.astuple(Some(1))
(1,)

julia> Try.astuple(nothing)
()
```