Skip to content

Instantly share code, notes, and snippets.

@shashi
Last active December 27, 2016 17:25
Show Gist options
  • Save shashi/b153d0dbe52f3fbedd7d0c2db3092bf8 to your computer and use it in GitHub Desktop.
Save shashi/b153d0dbe52f3fbedd7d0c2db3092bf8 to your computer and use it in GitHub Desktop.
immutable IRef{T}
value::T
IRef() = new()
IRef(value) = new(value)
end
immutable Result{T,S}
issuccess::Bool
value::IRef{T}
error::IRef{S}
function Result(issuccess, val)
issuccess ?
new(issuccess, IRef{T}(val), IRef{S}()) :
new(issuccess, IRef{T}(), IRef{S}(val))
end
end
immutable FailedResult end
immutable SuccessResult end
issuccess(e::Result) = e.issuccess
value(e::Result) = e.issuccess ? e.value.value : throw(FailedResult())
error(e::Result) = e.issuccess ? throw(SuccessResult()) : e.error.value
#=
using BenchmarkTools
# Construction
julia> @benchmark Nullable{Float64}()
BenchmarkTools.Trial:
memory estimate: 0.00 bytes
allocs estimate: 0
--------------
minimum time: 2.176 ns (0.00% GC)
median time: 2.257 ns (0.00% GC)
mean time: 2.245 ns (0.00% GC)
maximum time: 13.418 ns (0.00% GC)
--------------
samples: 10000
evals/sample: 1000
time tolerance: 5.00%
memory tolerance: 1.00%
julia> @benchmark Nullable{Int}(0)
BenchmarkTools.Trial:
memory estimate: 0.00 bytes
allocs estimate: 0
--------------
minimum time: 2.532 ns (0.00% GC)
median time: 2.626 ns (0.00% GC)
mean time: 2.600 ns (0.00% GC)
maximum time: 15.280 ns (0.00% GC)
--------------
samples: 10000
evals/sample: 1000
time tolerance: 5.00%
memory tolerance: 1.00%
julia> @benchmark Result{Int,Float64}(true, 1)
BenchmarkTools.Trial:
memory estimate: 0.00 bytes
allocs estimate: 0
--------------
minimum time: 2.530 ns (0.00% GC)
median time: 2.624 ns (0.00% GC)
mean time: 2.599 ns (0.00% GC)
maximum time: 42.189 ns (0.00% GC)
--------------
samples: 10000
evals/sample: 1000
time tolerance: 5.00%
memory tolerance: 1.00%
julia> @benchmark Result{Int,Float64}(false, 1.0)
BenchmarkTools.Trial:
memory estimate: 0.00 bytes
allocs estimate: 0
--------------
minimum time: 2.175 ns (0.00% GC)
median time: 2.259 ns (0.00% GC)
mean time: 2.256 ns (0.00% GC)
maximum time: 15.364 ns (0.00% GC)
--------------
samples: 10000
evals/sample: 1000
time tolerance: 5.00%
memory tolerance: 1.00%
# accessors
julia> @benchmark get($(Nullable{Int}(0)))
BenchmarkTools.Trial:
memory estimate: 0.00 bytes
allocs estimate: 0
--------------
minimum time: 2.177 ns (0.00% GC)
median time: 2.259 ns (0.00% GC)
mean time: 2.243 ns (0.00% GC)
maximum time: 17.017 ns (0.00% GC)
--------------
samples: 10000
evals/sample: 1000
time tolerance: 5.00%
memory tolerance: 1.00%
julia> @benchmark value($(Result{Int, Float64}(true,1)))
BenchmarkTools.Trial:
memory estimate: 0.00 bytes
allocs estimate: 0
--------------
minimum time: 2.529 ns (0.00% GC)
median time: 2.642 ns (0.00% GC)
mean time: 2.610 ns (0.00% GC)
maximum time: 15.069 ns (0.00% GC)
--------------
samples: 10000
evals/sample: 1000
time tolerance: 5.00%
memory tolerance: 1.00%
julia> @benchmark error($(Result{Int, Float64}(false,1.0)))
BenchmarkTools.Trial:
memory estimate: 0.00 bytes
allocs estimate: 0
--------------
minimum time: 2.445 ns (0.00% GC)
median time: 2.632 ns (0.00% GC)
mean time: 2.613 ns (0.00% GC)
maximum time: 13.549 ns (0.00% GC)
--------------
samples: 10000
evals/sample: 1000
time tolerance: 5.00%
memory tolerance: 1.00%
=#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment