Skip to content

Commit

Permalink
Update for Julia 0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
iamed2 committed Feb 9, 2018
1 parent 151d1e1 commit 7b32460
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
2 changes: 2 additions & 0 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
julia 0.6
Nullables 0.0.3
Compat 0.33
10 changes: 7 additions & 3 deletions src/ResultTypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ __precompile__()

module ResultTypes

using Nullables

export Result, ErrorResult, unwrap, unwrap_error, iserror

struct Result{T, E<:Exception}
Expand All @@ -20,7 +22,7 @@ function ErrorResult(::Type{T}, e::AbstractString="") where T
Result{T, ErrorException}(Nullable{T}(), Nullable{ErrorException}(ErrorException(e)))
end

function unwrap{T, E}(r::Result{T, E})::T
function unwrap(r::Result{T, E})::T where {T, E}
if !isnull(r.result)
return get(r.result)
elseif !isnull(r.error)
Expand All @@ -30,15 +32,17 @@ function unwrap{T, E}(r::Result{T, E})::T
end
end

function unwrap_error{T, E}(r::Result{T, E})::E
function unwrap_error(r::Result{T, E})::E where {T, E}
if !isnull(r.error)
return get(r.error)
else
error("$r is not an ErrorResult")
end
end

Base.convert{T, S, E}(::Type{T}, r::Result{S, E})::T = unwrap(r)
function Base.convert(::Type{T}, r::Result{S, E})::T where {T, S, E}
unwrap(r)
end

function Base.convert(::Type{Result{S, E}}, x::T) where {T, S, E}
Result{S, E}(Nullable{S}(convert(S, x)), Nullable{E}())
Expand Down
15 changes: 10 additions & 5 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using ResultTypes
using Base.Test
using Compat.Test
using Nullables

@testset "ResultTypes" begin

@testset "Result" begin
@testset "Basic result" begin
Expand Down Expand Up @@ -52,14 +55,14 @@ end
@testset "Convert" begin
@testset "From Result" begin
x = Result(2)
@test Int(x) === 2
@test Float64(x) === 2.0
@test_throws MethodError String(x)
@test convert(Int, x) === 2
@test convert(Float64, x) === 2.0
@test_throws MethodError convert(String, x)
end

@testset "From ErrorResult" begin
x = ErrorResult(Int, "Foo")
@test_throws ErrorException Int(x)
@test_throws ErrorException convert(Int, x)
end

@testset "To Result" begin
Expand Down Expand Up @@ -107,3 +110,5 @@ end
@test isa(string(x), String)
end
end

end

0 comments on commit 7b32460

Please sign in to comment.