Skip to content

Commit

Permalink
Fix conversion of Ok
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobnissen committed Feb 8, 2021
1 parent 92b887f commit 0c2587f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ErrorTypes"
uuid = "7f495686-c6d2-4c77-9e8e-e4c865675f9d"
authors = ["Jakob Nybo Nissen <jakobnybonissen@gmail.com>"]
version = "0.1.0"
version = "0.2.0"

[deps]
SumTypes = "8e1ec7a9-0e02-4297-b0fe-6433085c89f2"
Expand Down
6 changes: 5 additions & 1 deletion src/ErrorTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ end
# Convert an error to another error if the former is a subtype of the other
function Base.convert(::Type{Result{O1, E1}}, x::Result{O2, E2}
) where {O1, E1, O2 <: O1, E2 <: E1}
Err{O1, E1}(x.data._1)
if x.data isa Err
Err{O1, E1}(x.data._1)
else
Ok{O1, E1}(x.data._1)
end
end

# Convert Result value that contains an Err, even if the Ok parameter
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ end

# convert a result directly to another if all the subtypes match
@test convert(Result{Integer, AbstractString}, Err{Int, String}("boo")) isa Result{Integer, AbstractString}
@test convert(Result{Signed, String}, Ok{Int32, String}(Int32(9))) isa Result{Signed, String}
@test_throws MethodError convert(Result{Dict, Array}, Ok{Int, Bool}(1))

# convert Result values with differing params. It works if the param of
Expand Down

0 comments on commit 0c2587f

Please sign in to comment.