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

Add show methods #30

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
3 changes: 1 addition & 2 deletions src/Try.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,10 @@ include("ExternalDocstrings.jl")
using .ExternalDocstrings: @define_docstrings

include("core.jl")
include("show.jl")
include("errortrace.jl")
include("function.jl")

include("branch.jl")
include("show.jl")

end # module Internal

Expand Down
29 changes: 23 additions & 6 deletions src/function.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,34 @@ macro define_function(name::Symbol)
end |> esc
end

(fn::Tryable)(args...; kwargs...) = Err(Try.NotImplementedError(fn, args, kwargs))
(fn::Tryable)(args...; kwargs...) = Err(Try.NotImplementedError(fn, args, values(kwargs)))

struct NotImplementedError{T} <: Try.NotImplementedError end
struct NotImplementedError{F,Args<:Tuple,Kwargs<:NamedTuple} <: Try.NotImplementedError
f::F
args::Args
kwargs::Kwargs
end
# TODO: check if it is better to "type-erase"
# TODO: don't ignore kwargs?
# TODO: don't capture values?

asnamedtuple(kwargs::NamedTuple) = kwargs
asnamedtuple(kwargs) = (; kwargs...)

Try.NotImplementedError(f, args, _kwargs) =
NotImplementedError{Tuple{_typesof(f, args...)...}}()
Try.NotImplementedError(
f,
args::Tuple,
kwargs::Union{NamedTuple,Iterators.Pairs} = NamedTuple(),
) = NotImplementedError(f, args, asnamedtuple(kwargs))

_typesof() = ()
_typesof(::Type{Head}, tail...) where {Head} = (Type{Head}, _typesof(tail...)...)
_typesof(head, tail...) = (typeof(head), _typesof(tail...)...)

# TODO: show methods
Base.print(io::IO, fn::Tryable) = print(io, nameof(fn))
Base.show(io::IO, fn::Tryable) = print(io, nameof(fn))

function Base.show(io::IO, ::MIME"text/plain", fn::Tryable)
print(io, nameof(fn))
n = length(methods(fn))
print(io, " (tryable function with ", n, " method", n == 1 ? "" : "s", ")")
end
30 changes: 30 additions & 0 deletions src/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,33 @@ function Base.show(io::IO, ::MIME"text/plain", err::Err)
showerror(io, ex, simplify_backtrace(err.backtrace))
end
end

# TODO: simplify arguments when they are too long
function Base.showerror(io::IO, ex::NotImplementedError)
print(io, "Not Implemented: ")
show(io, ex.f)
print(io, '(')
let isfirst = true
for a in ex.args
if isfirst
isfirst = false
else
print(io, ", ")
end
show(IOContext(io, :compact => true, :limit => true), a)
end
end
let isfirst = true
for (k, v) in pairs(ex.kwargs)
if isfirst
isfirst = false
print(io, "; ")
else
print(io, ", ")
end
print(io, k, " = ")
show(IOContext(io, :compact => true, :limit => true), v)
end
end
print(io, ')')
end
1 change: 1 addition & 0 deletions test/TryTests/src/TryTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ include("test_base.jl")
include("test_errortrace.jl")
include("test_tools.jl")
include("test_inferrability.jl")
include("test_show.jl")
include("test_doctest.jl")

end # module TryTests
20 changes: 20 additions & 0 deletions test/TryTests/src/test_show.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module TestShow

using Test
using Try

Try.@function dummy

function test_tryable()
@test string(dummy) == "dummy"
@test sprint(show, dummy) == "dummy"
@test sprint(show, "text/plain", dummy) == "dummy (tryable function with 1 method)"
end

function test_notimplementederror()
ex = Try.NotImplementedError(identity, (1, 2, 3), (a = 4, b = 5))
msg = sprint(showerror, ex)
@test occursin("identity(1, 2, 3; a = 4, b = 5)", msg)
end

end # module