Skip to content

Commit f18d92a

Browse files
committed
Add Try.astuple
1 parent abbf34e commit f18d92a

4 files changed

Lines changed: 36 additions & 0 deletions

File tree

docs/src/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Try.unwrap_err
1616
Try.oktype
1717
Try.errtype
1818
Try.map
19+
Try.astuple
1920
```
2021

2122
## Short-circuit evaluation

src/Try.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct IsOkError <: InternalPrelude.Exception
3535
end
3636

3737
function map end
38+
function astuple end
3839

3940
macro and_return end
4041
function var"@?" end

src/branch.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@ macro or(ex, rest...)
161161
end
162162
end
163163

164+
function Try.astuple(result)
165+
br = branch(result)
166+
if br isa Break
167+
()
168+
else
169+
(valueof(br),)
170+
end
171+
end
172+
164173
###
165174
### Currying
166175
###

src/docs/astuple.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Try.astuple(result) -> (value,) or ()
2+
Try.astuple(Ok(value)) -> (value,)
3+
Try.astuple(::Err) -> ()
4+
Try.astuple(Some(value)) -> (value,)
5+
Try.astuple(nothing) -> ()
6+
7+
Return a singleton tuple with the value if the result is "successful"; return an empty tuple
8+
otherwise.
9+
10+
# Examples
11+
```julia
12+
julia> using Try
13+
14+
julia> Try.astuple(Ok(1))
15+
(1,)
16+
17+
julia> Try.astuple(Err(1))
18+
()
19+
20+
julia> Try.astuple(Some(1))
21+
(1,)
22+
23+
julia> Try.astuple(nothing)
24+
()
25+
```

0 commit comments

Comments
 (0)