Skip to content

Define Base.show for SArray and MArray. #1021

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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/MArray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ mutable struct MArray{S <: Tuple, T, N, L} <: StaticArray{S, T, N}
end
end

function Base.show(io::IO, a::MArray{S,T}) where {S,T}
print(io, 'M')
_show_shape_size_type(io, size(a), T)
print(io, Tuple(a))
end

@generated function (::Type{MArray{S,T,N}})(x::Tuple) where {S,T,N}
return quote
$(Expr(:meta, :inline))
Expand Down
6 changes: 6 additions & 0 deletions src/SArray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ struct SArray{S <: Tuple, T, N, L} <: StaticArray{S, T, N}
end
end

function Base.show(io::IO, a::SArray{S,T}) where {S,T}
print(io, 'S')
_show_shape_size_type(io, size(a), T)
print(io, Tuple(a))
end

@generated function (::Type{SArray{S, T, N}})(x::Tuple) where {S <: Tuple, T, N}
return quote
@_inline_meta
Expand Down
24 changes: 24 additions & 0 deletions src/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,27 @@ Base.@propagate_inbounds function invperm(p::StaticVector)
similar_type(p)(ip)
end

"""
_show_shape_size_type(io, S)

Utility function for printing SArray and MArray types. Caller should print the `'S'` or the
`'M'`, then this function prints the shape name, an opening curly brace, and the size
parameters, the type parameter, and the closing curly brace in the format that can be used
to construct the type.

NOTE: does not special-case 0-dimensional arrays ([`Scalar`](@ref)).
"""
function _show_shape_size_type(io::IO, S::Tuple, ::Type{T}) where T
_show_shape_size(io, S)
print(io, ",", T, "}")
end

_show_shape_size(io::IO, S::NTuple{1}) = print(io, "Vector{$(S[1])")

_show_shape_size(io::IO, S::NTuple{2}) = print(io, "Matrix{$(S[1]),$(S[2])")

function _show_shape_size(io::IO, S::Tuple)
print(io, "Array{Tuple{")
join(io, S, ',')
print(io, "}")
end
11 changes: 11 additions & 0 deletions test/MArray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,15 @@
v[] = 2
@test v[] == 2
end

@testset "repr and show roundtrip" begin
z = MArray{Tuple{}}(1.0)
v = MVector{8}(float.(1:8))
m = MMatrix{2,4}(v)
a = MArray{Tuple{2,2,2}}(v)
for x in (z, v, m, a)
z = eval(Meta.parse(repr(x)))
@test z isa MArray && x == z && size(x) == size(z) && eltype(x) == eltype(z)
end
end
end
10 changes: 10 additions & 0 deletions test/SArray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,14 @@
@test @inferred(promote_type(SVector{2,Int}, SVector{2,Float64})) === SVector{2,Float64}
@test @inferred(promote_type(SMatrix{2,3,Float32,6}, SMatrix{2,3,Complex{Float64},6})) === SMatrix{2,3,Complex{Float64},6}
end

@testset "repr and show roundtrip" begin
z = SArray{Tuple{}}(1.0)
v = SVector{8}(float.(1:8))
m = SMatrix{2,4}(v)
a = SArray{Tuple{2,2,2}}(v)
for x in (z, v, m, a)
@test eval(Meta.parse(repr(x))) ≡ x
end
end
end