diff --git a/src/oneelement.jl b/src/oneelement.jl index 0498f154..1ce02ef2 100644 --- a/src/oneelement.jl +++ b/src/oneelement.jl @@ -392,19 +392,23 @@ end # show _maybesize(t::Tuple{Base.OneTo{Int}, Vararg{Base.OneTo{Int}}}) = size.(t,1) _maybesize(t) = t -Base.show(io::IO, A::OneElement) = print(io, OneElement, "(", A.val, ", ", A.ind, ", ", _maybesize(axes(A)), ")") -function Base.show(io::IO, A::OneElement{<:Any,1,Tuple{Int},Tuple{Base.OneTo{Int}}}) +function Base.show(io::IO, @nospecialize(A::OneElement)) + # We always print the inds and axes (or size, for Base.OneTo axes) + # We print the value only if it isn't 1 + # this way, we have at least two arguments displayed that are unambiguous print(io, OneElement) - if eltype(A) != Int + isvector = ndims(A) == 1 + sz = _maybesize(axes(A)) + hasstandardaxes = sz isa Tuple{Vararg{Integer}} + isstandardvector = isvector & hasstandardaxes + if hasstandardaxes && eltype(A) != Int && isone(A.val) print(io, "{", eltype(A), "}") end print(io, "(") - if !isone(A.val) + if !(hasstandardaxes && isone(A.val)) print(io, A.val, ", ") end - print(io, A.ind[1]) - if A.ind[1] != size(A,1) - print(io, ", ", size(A,1)) - end + print(io, isstandardvector ? A.ind[1] : A.ind, ", ") + print(io, isstandardvector ? sz[1] : sz) print(io, ")") end diff --git a/test/runtests.jl b/test/runtests.jl index 380c15f8..09174e1a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -2346,6 +2346,31 @@ end @test repr(B) == "OneElement(2, 1, 3)" B = OneElement(2, (1, 2), (Base.IdentityUnitRange(1:1), Base.IdentityUnitRange(2:2))) @test repr(B) == "OneElement(2, (1, 2), (Base.IdentityUnitRange(1:1), Base.IdentityUnitRange(2:2)))" + + B = OneElement(2.0, (1, 2), (3, 4)) + @test repr(B) == "OneElement(2.0, (1, 2), (3, 4))" + B = OneElement(2.0, 1, 3) + @test repr(B) == "OneElement(2.0, 1, 3)" + B = OneElement(2.0, (1, 2), (Base.IdentityUnitRange(1:1), Base.IdentityUnitRange(2:2))) + @test repr(B) == "OneElement(2.0, (1, 2), (Base.IdentityUnitRange(1:1), Base.IdentityUnitRange(2:2)))" + + B = OneElement((1, 2), (3, 4)) + @test repr(B) == "OneElement((1, 2), (3, 4))" + B = OneElement((1, 2)) + @test repr(B) == "OneElement((1, 2), (1, 2))" + B = OneElement(2, 3) + @test repr(B) == "OneElement(2, 3)" + B = OneElement(2) + @test repr(B) == "OneElement(2, 2)" + + B = OneElement{Bool}((1, 2), (3, 4)) + @test repr(B) == "OneElement{Bool}((1, 2), (3, 4))" + B = OneElement(1.0, (1, 2), (3, 4)) + @test repr(B) == "OneElement{Float64}((1, 2), (3, 4))" + B = OneElement{Bool}(2, 3) + @test repr(B) == "OneElement{Bool}(2, 3)" + B = OneElement(1.0, 2, 3) + @test repr(B) == "OneElement{Float64}(2, 3)" end end