Skip to content

Commit fabe3a4

Browse files
committed
Minor renames of functions
1 parent 6578f20 commit fabe3a4

File tree

4 files changed

+70
-53
lines changed

4 files changed

+70
-53
lines changed

Diff for: NEWS.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## Release 0.5.0
2+
3+
__Breaking changes__
4+
* Renamed `unwrap_err` and `expect_err` to `unwrap_error` and `expect_error`.
5+
6+
__New features__
7+
* `@unwrap_error_or` and `unwrap_error_or` mirrors their non-error equivalents.
8+
19
## Release 0.4
210
__Breaking changes__
311
ErrorTypes is no longer based on SumTypes. SumTypes tries to solve a more general problem, and as such requires more compiler tricks. That both obfuscated ErrorTypes' behaviour and made it a more heavy dependency than it needed to be.

Diff for: Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ErrorTypes"
22
uuid = "7f495686-c6d2-4c77-9e8e-e4c865675f9d"
33
authors = ["Jakob Nybo Nissen <[email protected]>"]
4-
version = "0.4.0"
4+
version = "0.5.0"
55

66
[compat]
77
julia = "1.3"

Diff for: src/ErrorTypes.jl

+39-43
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,7 @@ end
4848

4949
function Result{T1, E1}(x::Result{T2, E2}) where {T1, T2 <: T1, E1, E2 <: E1}
5050
inner = x.x
51-
if inner isa Err
52-
Result{T1, E1}(Err{E1}(unsafe, inner.x))
53-
else
54-
Result{T1, E1}(Ok{T1}(unsafe, inner.x))
55-
end
51+
return Result{T1, E1}(inner isa Err ? Err(inner.x) : Ok(inner.x))
5652
end
5753

5854
function Base.convert(::Type{Result{T1, E1}}, x::Result{T2, E2}) where {T1, T2 <: T1, E1, E2 <: E1}
@@ -88,12 +84,7 @@ is_error(x::Result) = x.x isa Err
8884
Option(x::Result{T, E}) where {T, E} = is_error(x) ? none(T) : Option{T}(x.x)
8985
function Result(x::Option{T}, v::E) where {T, E}
9086
data = x.x
91-
inner = if x.x isa Err
92-
Err{E}(unsafe, v)
93-
else
94-
x.x
95-
end
96-
return Result{T, E}(inner)
87+
return Result{T, E}(data isa Err ? Err(v) : x.x)
9788
end
9889

9990
some(x::T) where T = Option{T}(Ok{T}(unsafe, x))
@@ -141,6 +132,15 @@ macro var"?"(expr)
141132
end
142133
end
143134

135+
macro unwrap_t_or(expr, T, exec)
136+
quote
137+
local res = $(esc(expr))
138+
isa(res, Result) || throw(TypeError(Symbol("@unwrap_or"), Result, res))
139+
local data = res.x
140+
data isa $(esc(T)) ? data.x : $(esc(exec))
141+
end
142+
end
143+
144144
# Note: jldoctests crashes on this ATM, even if it works correctly.
145145
"""
146146
@unwrap_or(expr, exec)
@@ -166,12 +166,16 @@ julia> skip_inv_sum([2,1,0,1,2])
166166
```
167167
"""
168168
macro unwrap_or(expr, exec)
169-
quote
170-
local res = $(esc(expr))
171-
isa(res, Result) || throw(TypeError(Symbol("@unwrap_or"), Result, res))
172-
local data = res.x
173-
data isa Ok ? data.x : $(esc(exec))
174-
end
169+
return :(@unwrap_t_or $(esc(expr)) Ok $(esc(exec)))
170+
end
171+
172+
"""
173+
@unwrap_error_or(expr, exec)
174+
175+
Same as `@unwrap_or`, but unwraps errors.
176+
"""
177+
macro unwrap_error_or(expr, exec)
178+
return :(@unwrap_t_or $(esc(expr)) Err $(esc(exec)))
175179
end
176180

177181
@noinline throw_unwrap_err() = error("unwrap on unexpected type")
@@ -182,42 +186,30 @@ end
182186
If `x` is of the associated error type, throw an error. Else, return the contained
183187
result type.
184188
"""
185-
function unwrap(x::Result)
186-
data = x.x
187-
data isa Ok ? data.x : throw_unwrap_err()
188-
end
189+
unwrap(x::Result) = @unwrap_or x throw_unwrap_err()
189190

190191
"""
191-
unwrap_err(x::Result)
192+
unwrap_error(x::Result)
192193
193194
If `x` contains an `Err`, return the content of the `Err`. Else, throw an error.
194195
"""
195-
function unwrap_err(x::Result)
196-
data = x.x
197-
data isa Err ? data.x : throw_unwrap_err()
198-
end
196+
unwrap_error(x::Result) = @unwrap_error_or x throw_unwrap_err()
199197

200198
"""
201199
expect(x::Result, s::AbstractString)
202200
203201
If `x` is of the associated error type, error with message `s`. Else, return
204202
the contained result type.
205203
"""
206-
function expect(x::Result, s::AbstractString)
207-
data = x.x
208-
data isa Ok ? data.x : error(s)
209-
end
204+
expect(x::Result, s::AbstractString) = @unwrap_or x error(s)
210205

211206
"""
212-
expect_err(x::Result, s::AbstractString)
207+
expect_error(x::Result, s::AbstractString)
213208
214209
If `x` contains an `Err`, return the content of the `Err`. Else, throw an error
215210
with message `s`.
216211
"""
217-
function expect_err(x::Result, s::AbstractString)
218-
data = x.x
219-
data isa Err ? data.x : error(s)
220-
end
212+
expect_error(x::Result, s::AbstractString) = @unwrap_error_or x error(s)
221213

222214
"""
223215
and_then(f, ::Type{T}, x::Result{O, E})
@@ -229,11 +221,7 @@ If `f(unwrap(x))` is not a `T`, this functions throws an error.
229221
"""
230222
function and_then(f, ::Type{T}, x::Result{O, E})::Result{T, E} where {T, O, E}
231223
data = x.x
232-
if data isa Ok
233-
Result{T, E}(Ok{T}(unsafe, f(data.x)))
234-
else
235-
Result{T, E}(Err{E}(unsafe, data.x))
236-
end
224+
Result{T, E}(data isa Ok ? Ok(f(data.x)) : Err(data.x))
237225
end
238226

239227
"""
@@ -243,6 +231,14 @@ If `x` is an error value, return `v`. Else, unwrap `x` and return its content.
243231
"""
244232
unwrap_or(x::Result, v) = @unwrap_or x v
245233

234+
235+
"""
236+
unwrap_error_or(x::Result, v)
237+
238+
Like `unwrap_or`, but unwraps an error.
239+
"""
240+
unwrap_error_or(x::Result, v) = @unwrap_error_or x v
241+
246242
"""
247243
flatten(x::Option{Option{T}})
248244
@@ -269,8 +265,8 @@ base(x::Option{T}) where T = Some(@unwrap_or x (return nothing))
269265

270266
export Ok, Err, Result, Option,
271267
is_error, some, none,
272-
unwrap, unwrap_err, expect, expect_err,
273-
and_then, unwrap_or, flatten, base,
274-
@?, @unwrap_or
268+
unwrap, unwrap_error, expect, expect_error,
269+
and_then, unwrap_or, unwrap_error_or, flatten, base,
270+
@?, @unwrap_or, @unwrap_error_or
275271

276272
end

Diff for: test/runtests.jl

+22-9
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,35 @@ using Test
4747
@test_throws ErrorException unwrap(Result{Int, String}(Err("foo")))
4848
@test_throws ErrorException unwrap(Result{Bool, Bool}(Err(true)))
4949

50+
# unwrap_or
5051
@test unwrap_or(Result{Int, Float64}(Ok(5)), 11) === 5
5152
@test unwrap_or(Result{Nothing, Nothing}(Ok(nothing)), 1) === nothing
5253
@test unwrap_or(Result{String, Dict}(Err(Dict())), "hello") == "hello"
5354
@test unwrap_or(Result{Int, Int}(Err(1)), 1 + 1) === 2
55+
56+
# unwrap_error_or
57+
@test unwrap_error_or(Result{Integer, AbstractFloat}(Err(1.1)), 1.2) === 1.1
58+
@test unwrap_error_or(Result{UInt8, UInt8}(Err(0x00)), Dict()) === 0x00
59+
@test unwrap_error_or(Result{Dict, String}(Ok(Dict())), missing) === missing
60+
@test unwrap_error_or(Result{Int, UInt}(Ok(55)), UInt(1)) === UInt(1)
5461

5562
# expect
5663
@test expect(Result{Nothing, Int}(Ok(nothing)), "foo") === nothing
5764
@test expect(convert(Result{Int,Int}, Ok(17)), " ") === 17
5865
@test_throws ErrorException expect(Result{Int, UInt}(Err(UInt(11))), "bar")
5966
@test_throws ErrorException expect(Result{Bool, Bool}(Err(true)), "foo")
6067

61-
# unwrap_err
62-
@test unwrap_err(Result{String, UInt}(Err(UInt(19)))) == UInt(19)
63-
@test unwrap_err(Result{AbstractDict, String}(Err("bar"))) == "bar"
64-
@test_throws ErrorException unwrap_err(Result{Int, Int}(Ok(1)))
65-
@test_throws ErrorException unwrap_err(Result{String, Int}(Ok("bar")))
68+
# unwrap_error
69+
@test unwrap_error(Result{String, UInt}(Err(UInt(19)))) == UInt(19)
70+
@test unwrap_error(Result{AbstractDict, String}(Err("bar"))) == "bar"
71+
@test_throws ErrorException unwrap_error(Result{Int, Int}(Ok(1)))
72+
@test_throws ErrorException unwrap_error(Result{String, Int}(Ok("bar")))
6673

6774
# expect_err
68-
@test expect_err(Result{Vector, AbstractString}(Err("x")), "foo") == "x"
69-
@test expect_err(Result{Int, UInt8}(Err(0xa4)), "mistake!") == 0xa4
70-
@test_throws ErrorException expect_err(Result{Int, UInt8}(Ok(15)), "foobar")
71-
@test_throws ErrorException expect_err(Result{Vector, AbstractString}(Ok([])), "xx")
75+
@test expect_error(Result{Vector, AbstractString}(Err("x")), "foo") == "x"
76+
@test expect_error(Result{Int, UInt8}(Err(0xa4)), "mistake!") == 0xa4
77+
@test_throws ErrorException expect_error(Result{Int, UInt8}(Ok(15)), "foobar")
78+
@test_throws ErrorException expect_error(Result{Vector, AbstractString}(Ok([])), "xx")
7279

7380
# and_then
7481
@test and_then(x -> x + 1, Float64, Result{Float64, String}(Ok(5.0))) === Result{Float64, String}(Ok(6.0))
@@ -170,6 +177,7 @@ end
170177
end
171178

172179
@testset "@unwrap_or" begin
180+
# @unwrap_or
173181
not_zero(x::Int)::Option{Int} = iszero(x) ? none : some(1 + x)
174182

175183
function my_sum(f, it)
@@ -191,6 +199,11 @@ end
191199
@test my_sum(not_zero_two, [1,2,0,2,0,1]) === 3.0
192200

193201
@test_throws TypeError @unwrap_or (1 + 1) "foo"
202+
203+
# @unwrap_error_or
204+
@test (@unwrap_error_or none(Integer) missing) === nothing
205+
@test (@unwrap_error_or some(55) missing) === missing
206+
@test (@unwrap_error_or Result{UInt8, String}(Err("Someerr")) missing) == "Someerr"
194207
end
195208

196209
# I only have this to get 100% code coverage and make sure printing doesn't error

0 commit comments

Comments
 (0)