Show() and sisters

Hi, here is my code :

julia> using Primes

julia> f=factor(binomial(30,12))
3 ⋅ 5^2 ⋅ 7 ⋅ 13 ⋅ 19 ⋅ 23 ⋅ 29

julia> repr(f)
"Primes.Factorization(3=>1,5=>2,7=>1,13=>1,19=>1,23=>1,29=>1)"

julia> string(f)
"Primes.Factorization(3=>1,5=>2,7=>1,13=>1,19=>1,23=>1,29=>1)"

Primes.Factorization has a nice show() method, but how to build a string from this representation ?

julia> reprmime("text/plain", f)
"3 ⋅ 5^2 ⋅ 7 ⋅ 13 ⋅ 19 ⋅ 23 ⋅ 29"
3 Likes

Wow, thank you a lot, this function is well hidden in the doc !

I did not find it from the docs, if you ?repr it shows up in the search.

You’re right, but I used the doc, where repr and string are in
https://docs.julialang.org/en/stable/stdlib/strings/ (that looks fine :-),
whereas reprmime and stringmime are in
https://docs.julialang.org/en/stable/stdlib/io-network/ .

Anyway imho repr should be the good answer.

repr is equivalent to show(io, x), whereas reprmime (documented here) is equivalent to show(io, mime, x). The latter defaults to the former for text/plain, but it is sometimes desirable to distinguish the two, as described here: https://docs.julialang.org/en/stable/manual/types/#Custom-pretty-printing-1

Usually, you want repr(x) to be a form that you could use to enter the same object, i.e. includestring(repr(x)) should usually work. Whereas the text/plain version might be a more pretty-printed output that is designed for human consumption, not computer parsing.

That being said, I suspect that we should rename reprmime(mime, x) to repr(mime, x), just as writemime(io, mime, x) was renamed to show(io, mime, x).

4 Likes