Skip to content

Commit

Permalink
Use compact printing for Nullable
Browse files Browse the repository at this point in the history
Print the wrapped value compactly, as inside other containers,
since the type information is redundant with the type parameter.
Also print the type with :multiline=>false.
  • Loading branch information
nalimilan committed Jun 9, 2016
1 parent 4f47148 commit 14c477b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
6 changes: 4 additions & 2 deletions base/nullable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ function show{T}(io::IO, x::Nullable{T})
show(io, x.value)
end
else
print(io, "Nullable{", T, "}(")
print(io, "Nullable{")
showcompact(io, eltype(x))
print(io, "}(")
if !isnull(x)
show(io, x.value)
showcompact(io, x.value)
end
print(io, ')')
end
Expand Down
2 changes: 1 addition & 1 deletion base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1548,7 +1548,7 @@ function showcompact(io::IO, x)
show(IOContext(io, :multiline => false), x)
end
else
if get(io, :multiline, false)
if !get(io, :multiline, false)
show(IOContext(io, :compact => true), x)
else
show(IOContext(IOContext(io, :compact => true),
Expand Down
8 changes: 6 additions & 2 deletions test/nullable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ for (i, T) in enumerate(types)
show(io1, x1)
@test takebuf_string(io1) == @sprintf("Nullable{%s}()", T)
show(io1, x2)
show(io2, get(x2))
showcompact(io2, get(x2))
@test takebuf_string(io1) == @sprintf("Nullable{%s}(%s)", T, takebuf_string(io2))
show(io1, x3)
show(io2, get(x3))
showcompact(io2, get(x3))
@test takebuf_string(io1) == @sprintf("Nullable{%s}(%s)", T, takebuf_string(io2))

a1 = [x2]
Expand All @@ -103,6 +103,10 @@ for (i, T) in enumerate(types)
@sprintf("Nullable{%s}[%s]", string(T), takebuf_string(io2))
end

@enum NullableTestEnum a b
show(io1, Nullable(a))
@test takebuf_string(io1) == "Nullable{NullableTestEnum}(a)"

# showcompact(io::IO, x::Nullable)
io1 = IOBuffer()
io2 = IOBuffer()
Expand Down

0 comments on commit 14c477b

Please sign in to comment.