Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comparison with ResultTypes #1

Open
ararslan opened this issue Nov 4, 2018 · 2 comments
Open

Comparison with ResultTypes #1

ararslan opened this issue Nov 4, 2018 · 2 comments

Comments

@ararslan
Copy link

ararslan commented Nov 4, 2018

First of all, neat package!

Rust's Result type is mentioned in the README, so it may be worthwhile to compare and contrast this approach in the README with Eric Davies' Julia implementation, ResultTypes.jl.

@KristofferC
Copy link
Owner

Hadn't seen that. Looking at it, they are indeed eerily similar. Implementation is a bit different, I use

struct Expected{T,E}
    v::Union{T,E}
    ok::Bool
end

while ResultTypes uses

struct Result{T, E <: Exception}
    result::Union{Some{T}, Nothing}
    error::Union{E, Nothing}
end

@KristofferC
Copy link
Owner

Here is a benchmark:

julia> using ResultTypes, Expect, BenchmarkTools

julia> @noinline function rt_integer_division(x::Int, y::Int)::Result{Int, DivideError}
           if y == 0
               return DivideError()
           else
               return div(x, y)
           end
       end

julia> @noinline function expect_integer_division(x::Int, y::Int)::Expect{Int, DivideError}
           if y == 0
               return DivideError()
           else
               return div(x, y)
           end
       end

julia> @noinline function rt_func2(x, y)
           r = rt_integer_division(x, y)
           if ResultTypes.iserror(r)
               return 0
           else
               return unwrap(r)
           end
       end

julia> @noinline function expect_func2(x, y)
           r = safe_divide(x, y)
           if !isassigned(r)
               return 0
           else
               return r[]
           end
       end
julia> @btime for i = 1:10 rt_func2(3, i % 2) end
  79.357 ns (0 allocations: 0 bytes)

julia> @btime for i = 1:10 expect_func2(3, i % 2) end
  80.165 ns (0 allocations: 0 bytes)

julia> @btime rt_func2(3, 0)
  8.279 ns (0 allocations: 0 bytes)
0

julia> @btime expect_func2(3, 0)
  8.576 ns (0 allocations: 0 bytes)
0

So performance seems pretty much identical.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants