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

repr roundtripping. "print it like you build it" #692

Open
goretkin opened this issue Nov 19, 2019 · 2 comments · May be fixed by #1021 or #906
Open

repr roundtripping. "print it like you build it" #692

goretkin opened this issue Nov 19, 2019 · 2 comments · May be fixed by #1021 or #906
Labels
feature features and feature requests up for grabs Implement me, I'm yours!

Comments

@goretkin
Copy link

I'm not sure if there are any functions in the display/show/repr family that are supposed to obey it, but it would be nice if a_rt === a (to the extent that everything is bitstype) in the following

using StaticArrays

a = @SArray [(SA_F32[1,2,3]), SA_F32[2,3,4]]
@show typeof(a)
sa = repr(a)
@show sa
a_rt = eval(Meta.parse(sa))
@show typeof(a_rt)

output:

typeof(a) = SArray{Tuple{2},SArray{Tuple{3},Float32,1,3},1,2}
sa = "SArray{Tuple{3},Float32,1,3}[[1.0, 2.0, 3.0], [2.0, 3.0, 4.0]]"
typeof(a_rt) = Array{SArray{Tuple{3},Float32,1,3},1}

(related a bit to the conversation in #691.

@c42f c42f added the feature features and feature requests label Nov 20, 2019
@c42f
Copy link
Member

c42f commented Nov 20, 2019

Good issue, thanks! I certainly think we could try changing repr for SArray so that it parses back to being a static array using SA. Currently we have

julia> repr(SA[1,2,3])
"[1, 2, 3]"

julia> repr(SA[SA_F32[1,2,3], SA_F32[4,5,6]])
"SArray{Tuple{3},Float32,1,3}[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]"

which is compact but not entirely ideal when printing nested data structures. SA is relatively compact so we could try to get

julia> repr(SA[1,2,3])
"SA[1, 2, 3]"

julia> repr(SA[SA_F32[1,2,3], SA_F32[4,5,6]])
"SA[SA_F32[1.0, 2.0, 3.0], SA_F32[4.0, 5.0, 6.0]]"

or some similar printing which generates the correct type and doesn't imply creating any Array temporaries when it's evaluated. For other StaticArray subtypes we could fall back to using conversion (eg, MArray(SA[1 2; 3 4])) I guess though that's not exactly pretty...

Would you be interested in giving it a go? There's several functions to think about (print / repr / show / display) to compare to how Base handles these things.

@c42f c42f added the up for grabs Implement me, I'm yours! label Nov 20, 2019
@tpapp
Copy link
Contributor

tpapp commented Apr 19, 2022

Something as simple as

function Base.show(io::IO, a::SArray{S,T}) where {S,T}
    print(io, "SArray{$(S),$(T)}")
    print(io, Tuple(vec(a)))
end

could take care of this. Special casing SVector and SMatrix would make it nicer, and it generalizes easily for MArray.

I am happy to make a PR (with tests etc) if there is interest.

tpapp added a commit to tpapp/StaticArrays.jl that referenced this issue Apr 24, 2022
This allows SArray and MArray to be printed (show, repr, etc) and read
back into an object of the same type.

Fixes JuliaArrays#692.
@tpapp tpapp linked a pull request Apr 24, 2022 that will close this issue
@mcabbott mcabbott linked a pull request May 7, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature features and feature requests up for grabs Implement me, I'm yours!
Projects
None yet
3 participants