diff --git a/REQUIRE b/REQUIRE index 137767a..585e181 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1 +1,3 @@ julia 0.6 +Nullables 0.0.3 +Compat 0.33 diff --git a/src/ResultTypes.jl b/src/ResultTypes.jl index a5778e5..66fc2b5 100644 --- a/src/ResultTypes.jl +++ b/src/ResultTypes.jl @@ -2,6 +2,8 @@ __precompile__() module ResultTypes +using Nullables + export Result, ErrorResult, unwrap, unwrap_error, iserror struct Result{T, E<:Exception} @@ -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) @@ -30,7 +32,7 @@ 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 @@ -38,7 +40,9 @@ function unwrap_error{T, E}(r::Result{T, E})::E 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}()) diff --git a/test/runtests.jl b/test/runtests.jl index 895703f..87fedb7 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,5 +1,8 @@ using ResultTypes -using Base.Test +using Compat.Test +using Nullables + +@testset "ResultTypes" begin @testset "Result" begin @testset "Basic result" begin @@ -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 @@ -107,3 +110,5 @@ end @test isa(string(x), String) end end + +end