Skip to content

Commit bd1a496

Browse files
author
KristofferC
committed
print NamedTuple types using the macro format
1 parent 703b3f8 commit bd1a496

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

base/namedtuple.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -461,20 +461,20 @@ This macro gives a more convenient syntax for declaring `NamedTuple` types. It r
461461
type with the given keys and types, equivalent to `NamedTuple{(:key1, :key2, ...), Tuple{Type1,Type2,...}}`.
462462
If the `::Type` declaration is omitted, it is taken to be `Any`. The `begin ... end` form allows the
463463
declarations to be split across multiple lines (similar to a `struct` declaration), but is otherwise
464-
equivalent.
464+
equivalent. The `NamedTuple` macro is used when printing `NamedTuple` types to e.g. the REPL.
465465
466-
For example, the tuple `(a=3.1, b="hello")` has a type `NamedTuple{(:a, :b),Tuple{Float64,String}}`, which
466+
For example, the tuple `(a=3.1, b="hello")` has a type `NamedTuple{(:a, :b), Tuple{Float64, String}}`, which
467467
can also be declared via `@NamedTuple` as:
468468
469469
```jldoctest
470470
julia> @NamedTuple{a::Float64, b::String}
471-
NamedTuple{(:a, :b), Tuple{Float64, String}}
471+
@NamedTuple{a::Float64, b::String}
472472
473473
julia> @NamedTuple begin
474474
a::Float64
475475
b::String
476476
end
477-
NamedTuple{(:a, :b), Tuple{Float64, String}}
477+
@NamedTuple{a::Float64, b::String}
478478
```
479479
480480
!!! compat "Julia 1.5"

base/show.jl

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,11 +1060,12 @@ end
10601060
function show_datatype(io::IO, x::DataType, wheres::Vector{TypeVar}=TypeVar[])
10611061
parameters = x.parameters::SimpleVector
10621062
istuple = x.name === Tuple.name
1063+
isnamedtuple = x.name === typename(NamedTuple)
10631064
n = length(parameters)
10641065

10651066
# Print tuple types with homogeneous tails longer than max_n compactly using `NTuple` or `Vararg`
1066-
max_n = 3
10671067
if istuple
1068+
max_n = 3
10681069
taillen = 1
10691070
for i in (n-1):-1:1
10701071
if parameters[i] === parameters[n]
@@ -1090,10 +1091,31 @@ function show_datatype(io::IO, x::DataType, wheres::Vector{TypeVar}=TypeVar[])
10901091
end
10911092
print(io, "}")
10921093
end
1093-
else
1094-
show_type_name(io, x.name)
1095-
show_typeparams(io, parameters, (unwrap_unionall(x.name.wrapper)::DataType).parameters, wheres)
1094+
return
1095+
elseif isnamedtuple
1096+
print(io, "@NamedTuple{")
1097+
syms, types = parameters
1098+
first = true
1099+
if syms isa Tuple
1100+
for i in 1:length(syms)
1101+
if !first
1102+
print(io, ", ")
1103+
end
1104+
print(io, syms[i])
1105+
typ = types.parameters[i]
1106+
if typ !== Any
1107+
print(io, "::")
1108+
show(io, typ)
1109+
end
1110+
first = false
1111+
end
1112+
print(io, "}")
1113+
return
1114+
end
10961115
end
1116+
1117+
show_type_name(io, x.name)
1118+
show_typeparams(io, parameters, (unwrap_unionall(x.name.wrapper)::DataType).parameters, wheres)
10971119
end
10981120

10991121
function show_supertypes(io::IO, typ::DataType)

test/show.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,6 +1348,14 @@ test_repr("(:).a")
13481348
@test repr(Tuple{String, Int64, Int64, Int64}) == "Tuple{String, Int64, Int64, Int64}"
13491349
@test repr(Tuple{String, Int64, Int64, Int64, Int64}) == "Tuple{String, Vararg{Int64, 4}}"
13501350

1351+
# Test printing of NamedTuples using the macro syntax
1352+
@test repr(@NamedTuple{kw::Int64}) == "@NamedTuple{kw::Int64}"
1353+
@test repr(@NamedTuple{kw::Union{Float64, Int64}, kw2::Int64}) == "@NamedTuple{kw::Union{Float64, Int64}, kw2::Int64}"
1354+
@test repr(@NamedTuple{kw::@NamedTuple{kw2::Int64}}) == "@NamedTuple{kw::@NamedTuple{kw2::Int64}}"
1355+
@test repr(@NamedTuple{kw::NTuple{7, Int64}}) == "@NamedTuple{kw::NTuple{7, Int64}}"
1356+
@test repr(@NamedTuple{a::Float64, b}) == "@NamedTuple{a::Float64, b}"
1357+
1358+
13511359
@testset "issue #42931" begin
13521360
@test repr(NTuple{4, :A}) == "NTuple{4, :A}"
13531361
@test repr(NTuple{3, :A}) == "Tuple{:A, :A, :A}"

0 commit comments

Comments
 (0)