Compact show for SArray and MArray#906
Conversation
|
Hah I was going to have a go at this too, JuliaArrays/FillArrays.jl#144 is the other one... But I think they can be more compact, at least sometimes. Why not print Less sure about the M case, but perhaps |
|
Did we consider |
|
Here's a quick attempt to hook into the machinery... julia> function Base.typeinfo_prefix(io::IO, X::SArray)
typeinfo = get(io, :typeinfo, Any)::Type
if !(X isa typeinfo)
typeinfo = Any
end
# what the context already knows about the eltype of X:
eltype_ctx = Base.typeinfo_eltype(typeinfo)
eltype_X = eltype(X)
# Types hard-coded here are those which are created by default for a given syntax
if eltype_X == eltype_ctx
"SA", false
elseif eltype_X == Float32 # added special case
"SA_F32", false
elseif !isempty(X) && Base.typeinfo_implicit(eltype_X)
"SA", true
# elseif print_without_params(eltype_X) # not sure what this does!
# sprint(show_type_name, unwrap_unionall(eltype_X).name), false # Print "Array" rather than "Array{T,N}"
else
string("SA{", eltype_X, "}"), false
end
end
julia> (SA[1,2,3], SA{Int32}[1 2; 3 4], SA[1f0, 2f0])
(SA[1, 2, 3], SA{Int32}[1 2; 3 4], SA_F32[1.0, 2.0])
julia> (SA{Int}[], SA{Float64}[], SA[]) # empty
(SA{Int64}[], SA{Float64}[], SA{Union{}}[])
julia> (SVector{2}(Any[1, 2]), SA{Any}[1,2], SVector{3}(Any[1, :a, nothing])) # first really does have eltype Int
(SA[1, 2], SA{Any}[1, 2], SA{Any}[1, :a, nothing])
julia> repr(@SMatrix rand(10,10); context=(:limit => true, :compact => true))
"SA[0.262792 0.33571 … 0.432945 0.18245; 0.535286 0.995272 … 0.881322 0.00517454; … ; 0.908566 0.738296 … 0.0315535 0.46813; 0.602212 0.843338 … 0.209644 0.31736]"
julia> [SA[0f0,i] for i in 1:3] # not sure if this should omit eltype? Omit everything? When :compact => true?
3-element Vector{SVector{2, Float32}}:
SA[0.0, 1.0]
SA[0.0, 2.0]
SA[0.0, 3.0]
julia> ([SA[1f0,2f0], SA[3f0,4f0]], [SA[1f0,2f0], SA[3f0,4f0,5f0]]) # arrays of arrays
(SVector{2, Float32}[[1.0, 2.0], [3.0, 4.0]], SArray{S, Float32, 1} where S<:Tuple[SA[1.0, 2.0], SA[3.0, 4.0, 5.0]]) |
|
I'm wondering if I realize it is shorter and convenient for developers, but am unconvinced that it's the best choice for |
|
Much as I like You could still minimise down to |
Oh, so am I! Even julia> SVector{2}(Any[1,2])
2-element SVector{2, Int64} with indices SOneTo(2):
1
2
julia> convert(SVector{2}, Any[1,2])
2-element SVector{2, Int64} with indices SOneTo(2):
1
2
julia> Vector(Any[1,2])
2-element Vector{Any}:
1
2
julia> convert(Vector, Any[1,2])
2-element Vector{Any}:
1
2We should definitely this behavior - so don't feel afraid to implement |
|
Would you ever want an abstract type though? |
|
In my opinion, it's important to mimic While this is more important for |
Along the lines of JuliaLang/julia#40722, this changes the way
show(io, ::SArray)andshow(io, ::MArray)displays the contents. The type information printed as a prefix distinguishes these from other dense arrays. Also makes the output valid as a constructor.After this PR:
Edit: Closes #692