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

Move internal constructors to InternalPrelude #23

Merged
merged 1 commit into from
Mar 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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