From 25d98f9d5ebc3448d324650967fc21e24e13039a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20K=2E=20Papp?= Date: Sun, 24 Apr 2022 15:29:35 +0200 Subject: [PATCH] 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 #692. --- src/MArray.jl | 6 ++++++ src/SArray.jl | 6 ++++++ src/util.jl | 24 ++++++++++++++++++++++++ test/MArray.jl | 11 +++++++++++ test/SArray.jl | 10 ++++++++++ 5 files changed, 57 insertions(+) diff --git a/src/MArray.jl b/src/MArray.jl index bb68affe..23532073 100644 --- a/src/MArray.jl +++ b/src/MArray.jl @@ -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)) diff --git a/src/SArray.jl b/src/SArray.jl index 18807def..3f75d31d 100644 --- a/src/SArray.jl +++ b/src/SArray.jl @@ -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 diff --git a/src/util.jl b/src/util.jl index 727c81a0..7c79daf3 100644 --- a/src/util.jl +++ b/src/util.jl @@ -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 diff --git a/test/MArray.jl b/test/MArray.jl index 7ed945d6..b0c59ccb 100644 --- a/test/MArray.jl +++ b/test/MArray.jl @@ -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 diff --git a/test/SArray.jl b/test/SArray.jl index 202fff46..d1c809e1 100644 --- a/test/SArray.jl +++ b/test/SArray.jl @@ -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