From 449cab9e6513cb14e052cd28fa01e8222fff5e41 Mon Sep 17 00:00:00 2001 From: Kristoffer Date: Mon, 11 Nov 2024 13:35:10 +0100 Subject: [PATCH] fix 2-arg show to be parsable --- src/simdvec.jl | 27 +++++++++++++-------------- test/runtests.jl | 6 ++++++ 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/simdvec.jl b/src/simdvec.jl index cb2c8cd..0366903 100644 --- a/src/simdvec.jl +++ b/src/simdvec.jl @@ -79,20 +79,19 @@ Base.length(V::Vec) = length(typeof(V)) Base.size(V::Vec) = size(typeof(V)) Base.size(V::Vec, n::Integer) = size(typeof(V), n) -if VERSION <= v"1.4.0-rc1.0" - function Base.show(io::IO, v::Vec{N,T}) where {N,T} - print(io, "<$N x $T>[") - join(io, [x.value for x in v.data], ", ") - print(io, "]") - end -else - # This crashes on pre 1.4-rc2 - function Base.show(io::IO, v::Vec{N,T}) where {N,T} - io = IOContext(io, :typeinfo => eltype(v)) - print(io, "<$N x $T>[") - join(io, [sprint(show, x.value; context=io) for x in v.data], ", ") - print(io, "]") - end + +function Base.show(io::IO, ::MIME"text/plain", v::Vec{N,T}) where {N,T} + io = IOContext(io, :typeinfo => eltype(v)) + print(io, "<$N x $T>[") + join(io, [sprint(show, x.value; context=io) for x in v.data], ", ") + print(io, "]") +end + +function Base.show(io::IO, v::Vec{N,T}) where {N, T} + show(io, Vec{N,T}) + write(io, "(") + show(io, Tuple(v)) + write(io, ")") end @inline Base.checkbounds(v::Vec, i::IntegerTypes) = diff --git a/test/runtests.jl b/test/runtests.jl index 6ba4c3c..831684b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -997,4 +997,10 @@ llvm_ir(f, args) = sprint(code_llvm, f, Base.typesof(args...)) @test all(r == Vec(4.0, 3.0, 3.0, 4.0)) end + @testset "parsable repr" begin + v = Vec(1.0, 2.0, 3.0, 4.0) + v2 = eval(Meta.parse(repr(v))) + @test v === v2 + end + # end