48
48
49
49
function Result {T1, E1} (x:: Result{T2, E2} ) where {T1, T2 <: T1 , E1, E2 <: E1 }
50
50
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))
56
52
end
57
53
58
54
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
88
84
Option (x:: Result{T, E} ) where {T, E} = is_error (x) ? none (T) : Option {T} (x. x)
89
85
function Result (x:: Option{T} , v:: E ) where {T, E}
90
86
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)
97
88
end
98
89
99
90
some (x:: T ) where T = Option {T} (Ok {T} (unsafe, x))
@@ -141,6 +132,15 @@ macro var"?"(expr)
141
132
end
142
133
end
143
134
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
+
144
144
# Note: jldoctests crashes on this ATM, even if it works correctly.
145
145
"""
146
146
@unwrap_or(expr, exec)
@@ -166,12 +166,16 @@ julia> skip_inv_sum([2,1,0,1,2])
166
166
```
167
167
"""
168
168
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)))
175
179
end
176
180
177
181
@noinline throw_unwrap_err () = error (" unwrap on unexpected type" )
@@ -182,42 +186,30 @@ end
182
186
If `x` is of the associated error type, throw an error. Else, return the contained
183
187
result type.
184
188
"""
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 ()
189
190
190
191
"""
191
- unwrap_err (x::Result)
192
+ unwrap_error (x::Result)
192
193
193
194
If `x` contains an `Err`, return the content of the `Err`. Else, throw an error.
194
195
"""
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 ()
199
197
200
198
"""
201
199
expect(x::Result, s::AbstractString)
202
200
203
201
If `x` is of the associated error type, error with message `s`. Else, return
204
202
the contained result type.
205
203
"""
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)
210
205
211
206
"""
212
- expect_err (x::Result, s::AbstractString)
207
+ expect_error (x::Result, s::AbstractString)
213
208
214
209
If `x` contains an `Err`, return the content of the `Err`. Else, throw an error
215
210
with message `s`.
216
211
"""
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)
221
213
222
214
"""
223
215
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.
229
221
"""
230
222
function and_then (f, :: Type{T} , x:: Result{O, E} ):: Result{T, E} where {T, O, E}
231
223
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))
237
225
end
238
226
239
227
"""
@@ -243,6 +231,14 @@ If `x` is an error value, return `v`. Else, unwrap `x` and return its content.
243
231
"""
244
232
unwrap_or (x:: Result , v) = @unwrap_or x v
245
233
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
+
246
242
"""
247
243
flatten(x::Option{Option{T}})
248
244
@@ -269,8 +265,8 @@ base(x::Option{T}) where T = Some(@unwrap_or x (return nothing))
269
265
270
266
export Ok, Err, Result, Option,
271
267
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
275
271
276
272
end
0 commit comments