diff --git a/examples/inferrability.jl b/examples/inferrability.jl index 8a65b08..6946e70 100644 --- a/examples/inferrability.jl +++ b/examples/inferrability.jl @@ -2,17 +2,15 @@ module UnionTyped using Try using TryExperimental using TryExperimental: Maybe -g(xs) = Ok(xs) -f(xs) = g(xs) |> Try.and_then(xs -> trygetindex(xs, 1)) |> Maybe.ok +f(xs) = Ok(xs) |> Try.and_then(xs -> trygetindex(xs, 1)) |> Maybe.ok end # module UnionTyped module ConcretelyTyped using Try using TryExperimental -using TryExperimental: Maybe -g(xs) = Try.ConcreteOk(xs) -function trygetfirst(xs)::Try.ConcreteResult{eltype(xs),BoundsError} +using TryExperimental: ConcreteResult, Maybe +function trygetfirst(xs)::ConcreteResult{eltype(xs),BoundsError} trygetindex(xs, 1) end -f(xs) = g(xs) |> Try.and_then(trygetfirst) |> Maybe.ok +f(xs) = Ok(xs) |> Try.and_then(trygetfirst) |> Maybe.ok end # module diff --git a/lib/TryExperimental/src/TryExperimental.jl b/lib/TryExperimental/src/TryExperimental.jl index 4b8ef20..93b4cdc 100644 --- a/lib/TryExperimental/src/TryExperimental.jl +++ b/lib/TryExperimental/src/TryExperimental.jl @@ -38,6 +38,8 @@ abstract type EmptyError <: Exception end abstract type ClosedError <: Exception end # abstract type FullError <: Exception end +const ConcreteResult = Try.Internal.ConcreteResult + macro and_then end macro or_else end diff --git a/lib/TryExperimental/src/maybe.jl b/lib/TryExperimental/src/maybe.jl index 71fa692..bf193e1 100644 --- a/lib/TryExperimental/src/maybe.jl +++ b/lib/TryExperimental/src/maybe.jl @@ -1,4 +1,5 @@ -using Try: Try, Ok, Err, ConcreteResult +using Try: Try, Ok, Err +using ...TryExperimental: ConcreteResult Maybe.ok(result::Ok) = Some{Try.oktype(result)}(result.value) Maybe.ok(::Err) = nothing diff --git a/src/Try.jl b/src/Try.jl index a88d12f..0d81c9f 100644 --- a/src/Try.jl +++ b/src/Try.jl @@ -2,38 +2,20 @@ baremodule Try export @?, Ok, Err, Result -using Base: Base, Exception - module InternalPrelude -function _ConcreteResult end +abstract type AbstractResult{T,E} end function _IsOkError end end # module InternalPrelude -abstract type AbstractResult{T,E} end - -struct Ok{T} <: AbstractResult{T,Union{}} +struct Ok{T} <: InternalPrelude.AbstractResult{T,Union{}} value::T end -struct Err{E} <: AbstractResult{Union{},E} +struct Err{E} <: InternalPrelude.AbstractResult{Union{},E} value::E - backtrace::Union{Nothing,typeof(Base.backtrace())} + backtrace::Union{Nothing,typeof(InternalPrelude.backtrace())} end -const DynamicResult{T,E} = Union{Ok{T},Err{E}} - -struct ConcreteResult{T,E} <: AbstractResult{T,E} - value::DynamicResult{T,E} - - InternalPrelude._ConcreteResult(::Type{T}, ::Type{E}, value) where {T,E} = - new{T,E}(value) -end - -const ConcreteOk{T} = ConcreteResult{T,Union{}} -const ConcreteErr{E} = ConcreteResult{Union{},E} - -const Result{T,E} = Union{ConcreteResult{<:T,<:E},DynamicResult{<:T,<:E}} - function unwrap end function unwrap_err end @@ -49,13 +31,13 @@ function istryable end function var"@function" end # Core exceptions -struct IsOkError <: Exception - ok::AbstractResult +struct IsOkError <: InternalPrelude.Exception + ok::InternalPrelude.AbstractResult InternalPrelude._IsOkError(ok) = new(ok) end -abstract type NotImplementedError <: Exception end +abstract type NotImplementedError <: InternalPrelude.Exception end macro and_return end function var"@?" end @@ -66,21 +48,13 @@ function or_else end module Internal import ..Try: @and_return, @?, @function -using ..Try: - AbstractResult, - ConcreteErr, - ConcreteOk, - ConcreteResult, - DynamicResult, - Err, - Ok, - Result, - Try -using ..Try.InternalPrelude: _ConcreteResult, _IsOkError +using ..Try: Err, Ok, Try +using ..Try.InternalPrelude: AbstractResult, _IsOkError include("ExternalDocstrings.jl") using .ExternalDocstrings: @define_docstrings +include("concrete.jl") include("core.jl") include("show.jl") include("errortrace.jl") @@ -90,6 +64,8 @@ include("branch.jl") end # module Internal +const Result = Internal.Result + Internal.@define_docstrings end # baremodule Try diff --git a/src/concrete.jl b/src/concrete.jl new file mode 100644 index 0000000..51cf0f3 --- /dev/null +++ b/src/concrete.jl @@ -0,0 +1,11 @@ +const DynamicResult{T,E} = Union{Ok{T},Err{E}} + +function _ConcreteResult end + +struct ConcreteResult{T,E} <: AbstractResult{T,E} + value::DynamicResult{T,E} + + global _ConcreteResult(::Type{T}, ::Type{E}, value) where {T,E} = new{T,E}(value) +end + +const Result{T,E} = Union{ConcreteResult{<:T,<:E},DynamicResult{<:T,<:E}} diff --git a/src/core.jl b/src/core.jl index 94259ba..287dd2d 100644 --- a/src/core.jl +++ b/src/core.jl @@ -11,7 +11,7 @@ Try.unwrap_err(result::ConcreteResult) = Try.unwrap_err(result.value) Try.unwrap_err(ok::Ok) = throw(Try.IsOkError(ok)) Try.unwrap_err(err::Err) = err.value -_throw(err::ConcreteErr) = _throw(err.value) +_throw(err::ConcreteResult) = _throw(err.value) function _throw(err::Err) if err.backtrace === nothing throw(err.value) @@ -37,11 +37,6 @@ Base.convert(::Type{Err{E}}, err::Err) where {E} = Err{E}(err.value) _concrete(result::Ok) = _ConcreteResult(Try.oktype(result), Union{}, result) _concrete(result::Err) = _ConcreteResult(Union{}, Try.errtype(result), result) -Try.ConcreteOk(value) = _concrete(Ok(value)) -Try.ConcreteOk{T}(value) where {T} = _concrete(Ok{T}(value)) -Try.ConcreteErr(value) = _concrete(Err(value)) -Try.ConcreteErr{E}(value) where {E} = _concrete(Err{E}(value)) - Base.convert(::Type{ConcreteResult{T,E}}, result::Ok) where {T,E} = _ConcreteResult(T, E, convert(Ok{T}, result)) Base.convert(::Type{ConcreteResult{T}}, result::Ok) where {T} = diff --git a/src/docs/Err.md b/src/docs/Err.md index e69de29..eccbd46 100644 --- a/src/docs/Err.md +++ b/src/docs/Err.md @@ -0,0 +1 @@ + Err{E} <: Result{T,E} diff --git a/src/docs/Result.md b/src/docs/Result.md index ba94844..fc222ea 100644 --- a/src/docs/Result.md +++ b/src/docs/Result.md @@ -1 +1 @@ - Err{E} <: Result{T,E<:Exception} + Result{T,E}