ronisbr / PrettyTables.jl

Print data in formatted tables.
MIT License
391 stars 38 forks source link

Enable easier dispatch on `pretty_table` for custom types #219

Closed jakobjpeters closed 10 months ago

jakobjpeters commented 10 months ago

As a package author depending on PrettyTables, I want to define a method for two custom types. Method A transforms a into data that PrettyTables can use and calls pretty_table. Method B transforms b into an A and calls method A.

pretty_table(io::IO, a::A; kwargs...) = pretty_table(io, f(a); kwargs...)
pretty_table(io::IO, b::B; kwargs...) = pretty_table(io, A(b); kwargs...)

However, if I want io to default to stdout, I currently need to to write the following methods:

pretty_table(a::A; kwargs...) = pretty_table(stdout, f(a); kwargs...)
pretty_table(b::B; kwargs...) = pretty_table(stdout, A(b); kwargs...)

With this PR, those two methods are no longer necessary. This reduces code repetition and makes overloading pretty_table easier.

ronisbr commented 10 months ago

Hi!

Thanks! This approach was used initially, but it was leading to a HUGE time to print the first table. It seems a good time to revisit the problem and check if things were improved.

ronisbr commented 10 months ago

Seems ok! The errors are not related to this PR and there are only a slight increase in TPFT (less than 0.01s).