Skip to content

Commit

Permalink
Move internal constructors to InternalPrelude
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf committed Mar 20, 2022
1 parent dee441d commit f150dab
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/Try.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ export Ok, Err, Result

using Base: Base, Exception

module InternalPrelude
function _ConcreteResult end
function _IsOkError end
end # module InternalPrelude

abstract type AbstractResult{T,E} end

struct Ok{T} <: AbstractResult{T,Union{}}
Expand All @@ -17,11 +22,11 @@ end

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)

InternalPrelude._ConcreteResult(::Type{T}, ::Type{E}, value) where {T,E} =
new{T,E}(value)
end

const ConcreteOk{T} = ConcreteResult{T,Union{}}
Expand All @@ -44,7 +49,9 @@ function istryable end

# Core exceptions
struct IsOkError <: Exception
ok::AbstractResult{<:Any,Union{}}
ok::AbstractResult

InternalPrelude._IsOkError(ok) = new(ok)
end

abstract type NotImplementedError <: Exception end
Expand All @@ -70,8 +77,8 @@ using ..Try:
Err,
Ok,
Result,
Try,
_ConcreteResult
Try
using ..Try.InternalPrelude: _ConcreteResult, _IsOkError

using Base.Meta: isexpr

Expand Down
7 changes: 7 additions & 0 deletions src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ function _throw(err::Err)
end
end

function Try.IsOkError(ok)
if Try.iserr(ok)
error("unexpected error value: ", ok)
end
return _IsOkError(ok)
end

Base.convert(::Type{Ok{T}}, ok::Ok) where {T} = Ok{T}(ok.value)
Base.convert(::Type{Err{E}}, err::Err) where {E} = Err{E}(err.value)
# An interesting approach may be to simply throw the `err.value` if it is not a
Expand Down

0 comments on commit f150dab

Please sign in to comment.