Skip to content

anything is showable as text/csv #31037

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

Open
SimonDanisch opened this issue Feb 11, 2019 · 4 comments
Open

anything is showable as text/csv #31037

SimonDanisch opened this issue Feb 11, 2019 · 4 comments
Labels
display and printing Aesthetics and correctness of printed representations of objects. stdlib Julia's standard library

Comments

@SimonDanisch
Copy link
Contributor

SimonDanisch commented Feb 11, 2019

Any Julia type is showable as text/csv by default, which makes it hard to actually implement a display that supports the text/csv mime.
This happens because of this method:
https://github.com/JuliaLang/julia/blob/master/stdlib/DelimitedFiles/src/DelimitedFiles.jl#L828
What makes it worse is, that not even all types are actually showable:

struct Test end
Base.showable(MIME"text/csv"(), Test())
Base.show(stdout, MIME"text/csv"(), Test())
MethodError: no method matching iterate(::Test)
Closest candidates are:
  iterate(!Matched::Core.SimpleVector) at essentials.jl:568
  iterate(!Matched::Core.SimpleVector, !Matched::Any) at essentials.jl:568
  iterate(!Matched::ExponentialBackOff) at error.jl:199
  ...
#writedlm#14(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::Function, ::Base.TTY, ::Test, ::Char) at DelimitedFiles.jl:779
writedlm(::Base.TTY, ::Test, ::Char) at DelimitedFiles.jl:776
show(::Base.TTY, ::MIME{Symbol("text/csv")}, ::Test) at DelimitedFiles.jl:828
top-level scope at none:0
@nalimilan nalimilan added display and printing Aesthetics and correctness of printed representations of objects. stdlib Julia's standard library labels Feb 11, 2019
@nalimilan
Copy link
Member

That line should probably be restricted to AbstractArray. Also I think @JeffBezanson mentioned in another issue that the CSV printing code is simple enough that it could be included in Base, which would avoid what is technically type piracy.

@JeffBezanson
Copy link
Member

Yes, this was found by #30945. I agree it should be restricted to AbstractArray (maybe just AbstractVector and AbstractMatrix). However, the code deliberately implements and tests support for arbitrary iterators of iterators, so technically it's a loss of functionality. I highly doubt anybody is using it though. Would be good to think of another API for that.

@SimonDanisch
Copy link
Contributor Author

I just noticed, that there is isiterable, so we could define at least:

showable(::MIME"text/csv", x::T) where T = Base.isiterable(T) || T <: AbstractVector || T <: AbstractMatrix

@JeffBezanson
Copy link
Member

I don't think that's good enough. It needs to be an iterator of iterators of elements that can be reasonably printed as CSV, which is a pretty marginal use case for such an invasive method definition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
display and printing Aesthetics and correctness of printed representations of objects. stdlib Julia's standard library
Projects
None yet
Development

No branches or pull requests

3 participants