Skip to content

Commit

Permalink
Add show methods (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf authored Mar 21, 2022
1 parent ab235a1 commit e6ee547
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 8 deletions.
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

0 comments on commit e6ee547

Please sign in to comment.