Skip to content

Commit 4887c49

Browse files
nickrobinson251JeffBezanson
authored andcommitted
Parseable repr for undef (#33211)
1 parent 47f2800 commit 4887c49

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

base/show.jl

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# This file is a part of Julia. License is MIT: https://julialang.org/license
22

3-
show(io::IO, ::UndefInitializer) = print(io, "array initializer with undefined values")
3+
function show(io::IO, ::MIME"text/plain", u::UndefInitializer)
4+
print(io, u, ": array initializer with undefined values")
5+
end
46

57
# first a few multiline show functions for types defined before the MIME type:
68

test/show.jl

+15
Original file line numberDiff line numberDiff line change
@@ -1541,6 +1541,21 @@ end
15411541
Z = Array{Float64}(undef,0,0)
15421542
@test eval(Meta.parse(repr(Z))) == Z
15431543

1544+
@testset "show undef" begin
1545+
# issue #33204 - Parseable `repr` for `undef`
1546+
@test eval(Meta.parse(repr(undef))) == undef == UndefInitializer()
1547+
@test showstr(undef) == "UndefInitializer()"
1548+
@test occursin(repr(undef), replstr(undef))
1549+
@test occursin("initializer with undefined values", replstr(undef))
1550+
1551+
vec_undefined = Vector(undef, 2)
1552+
vec_initialisers = fill(undef, 2)
1553+
@test showstr(vec_undefined) == "Any[#undef, #undef]"
1554+
@test showstr(vec_initialisers) == "UndefInitializer[$undef, $undef]"
1555+
@test replstr(vec_undefined) == "2-element Array{Any,1}:\n #undef\n #undef"
1556+
@test replstr(vec_initialisers) == "2-element Array{UndefInitializer,1}:\n $undef\n $undef"
1557+
end
1558+
15441559
# issue #31065, do not print parentheses for nested dot expressions
15451560
@test sprint(Base.show_unquoted, :(foo.x.x)) == "foo.x.x"
15461561

0 commit comments

Comments
 (0)