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 ConcreteResult to TryExperimental #27

Merged
merged 1 commit into from
Mar 21, 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
10 changes: 4 additions & 6 deletions examples/inferrability.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions lib/TryExperimental/src/TryExperimental.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion lib/TryExperimental/src/maybe.jl
Original file line number Diff line number Diff line change
@@ -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
Expand Down
48 changes: 12 additions & 36 deletions src/Try.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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")
Expand All @@ -90,6 +64,8 @@ include("branch.jl")

end # module Internal

const Result = Internal.Result

Internal.@define_docstrings

end # baremodule Try
11 changes: 11 additions & 0 deletions src/concrete.jl
Original file line number Diff line number Diff line change
@@ -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}}
7 changes: 1 addition & 6 deletions src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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} =
Expand Down
1 change: 1 addition & 0 deletions src/docs/Err.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Err{E} <: Result{T,E}
2 changes: 1 addition & 1 deletion src/docs/Result.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Err{E} <: Result{T,E<:Exception}
Result{T,E}