Skip to content

Commit 25d98f9

Browse files
committed
Define Base.show for SArray and MArray.
This allows SArray and MArray to be printed (show, repr, etc) and read back into an object of the same type. Fixes JuliaArrays#692.
1 parent c09bc9a commit 25d98f9

File tree

5 files changed

+57
-0
lines changed

5 files changed

+57
-0
lines changed

src/MArray.jl

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ mutable struct MArray{S <: Tuple, T, N, L} <: StaticArray{S, T, N}
3636
end
3737
end
3838

39+
function Base.show(io::IO, a::MArray{S,T}) where {S,T}
40+
print(io, 'M')
41+
_show_shape_size_type(io, size(a), T)
42+
print(io, Tuple(a))
43+
end
44+
3945
@generated function (::Type{MArray{S,T,N}})(x::Tuple) where {S,T,N}
4046
return quote
4147
$(Expr(:meta, :inline))

src/SArray.jl

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ struct SArray{S <: Tuple, T, N, L} <: StaticArray{S, T, N}
2929
end
3030
end
3131

32+
function Base.show(io::IO, a::SArray{S,T}) where {S,T}
33+
print(io, 'S')
34+
_show_shape_size_type(io, size(a), T)
35+
print(io, Tuple(a))
36+
end
37+
3238
@generated function (::Type{SArray{S, T, N}})(x::Tuple) where {S <: Tuple, T, N}
3339
return quote
3440
@_inline_meta

src/util.jl

+24
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,27 @@ Base.@propagate_inbounds function invperm(p::StaticVector)
112112
similar_type(p)(ip)
113113
end
114114

115+
"""
116+
_show_shape_size_type(io, S)
117+
118+
Utility function for printing SArray and MArray types. Caller should print the `'S'` or the
119+
`'M'`, then this function prints the shape name, an opening curly brace, and the size
120+
parameters, the type parameter, and the closing curly brace in the format that can be used
121+
to construct the type.
122+
123+
NOTE: does not special-case 0-dimensional arrays ([`Scalar`](@ref)).
124+
"""
125+
function _show_shape_size_type(io::IO, S::Tuple, ::Type{T}) where T
126+
_show_shape_size(io, S)
127+
print(io, ",", T, "}")
128+
end
129+
130+
_show_shape_size(io::IO, S::NTuple{1}) = print(io, "Vector{$(S[1])")
131+
132+
_show_shape_size(io::IO, S::NTuple{2}) = print(io, "Matrix{$(S[1]),$(S[2])")
133+
134+
function _show_shape_size(io::IO, S::Tuple)
135+
print(io, "Array{Tuple{")
136+
join(io, S, ',')
137+
print(io, "}")
138+
end

test/MArray.jl

+11
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,15 @@
218218
v[] = 2
219219
@test v[] == 2
220220
end
221+
222+
@testset "repr and show roundtrip" begin
223+
z = MArray{Tuple{}}(1.0)
224+
v = MVector{8}(float.(1:8))
225+
m = MMatrix{2,4}(v)
226+
a = MArray{Tuple{2,2,2}}(v)
227+
for x in (z, v, m, a)
228+
z = eval(Meta.parse(repr(x)))
229+
@test z isa MArray && x == z && size(x) == size(z) && eltype(x) == eltype(z)
230+
end
231+
end
221232
end

test/SArray.jl

+10
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,14 @@
206206
@test @inferred(promote_type(SVector{2,Int}, SVector{2,Float64})) === SVector{2,Float64}
207207
@test @inferred(promote_type(SMatrix{2,3,Float32,6}, SMatrix{2,3,Complex{Float64},6})) === SMatrix{2,3,Complex{Float64},6}
208208
end
209+
210+
@testset "repr and show roundtrip" begin
211+
z = SArray{Tuple{}}(1.0)
212+
v = SVector{8}(float.(1:8))
213+
m = SMatrix{2,4}(v)
214+
a = SArray{Tuple{2,2,2}}(v)
215+
for x in (z, v, m, a)
216+
@test eval(Meta.parse(repr(x))) x
217+
end
218+
end
209219
end

0 commit comments

Comments
 (0)