Skip to content

Commit 8187fc4

Browse files
KristofferCKristofferC
authored andcommitted
print NamedTuple types using the macro format
Update base/show.jl Co-authored-by: Jameson Nash <[email protected]> fixup
1 parent 703b3f8 commit 8187fc4

File tree

4 files changed

+53
-18
lines changed

4 files changed

+53
-18
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+
syms, types = parameters
1097+
first = true
1098+
if syms isa Tuple && types isa DataType
1099+
print(io, "@NamedTuple{")
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)

doc/src/manual/types.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -978,35 +978,40 @@ alias for `Tuple{Vararg{T,N}}`, i.e. a tuple type containing exactly `N` element
978978

979979
Named tuples are instances of the [`NamedTuple`](@ref) type, which has two parameters: a tuple of
980980
symbols giving the field names, and a tuple type giving the field types.
981+
For convenience, `NamedTuple` types are printed using the [`@NamedTuple`](@ref) macro which provides a
982+
convenient `struct`-like syntax for declaring these types via `key::Type` declarations,
983+
where an omitted `::Type` corresponds to `::Any`.
984+
981985

982986
```jldoctest
983-
julia> typeof((a=1,b="hello"))
984-
NamedTuple{(:a, :b), Tuple{Int64, String}}
987+
julia> typeof((a=1,b="hello")) # prints in macro form
988+
@NamedTuple{a::Int64, b::String}
989+
990+
julia> NamedTuple{(:a, :b), Tuple{Int64, String}} # long form of the type
991+
@NamedTuple{a::Int64, b::String}
985992
```
986993

987-
The [`@NamedTuple`](@ref) macro provides a more convenient `struct`-like syntax for declaring
988-
`NamedTuple` types via `key::Type` declarations, where an omitted `::Type` corresponds to `::Any`.
994+
The `begin ... end` form of the `@NamedTuple` macro allows the declarations to be
995+
split across multiple lines (similar to a struct declaration), but is otherwise equivalent:
989996

990-
```jldoctest
991-
julia> @NamedTuple{a::Int, b::String}
992-
NamedTuple{(:a, :b), Tuple{Int64, String}}
993997

998+
```jldoctest
994999
julia> @NamedTuple begin
9951000
a::Int
9961001
b::String
9971002
end
998-
NamedTuple{(:a, :b), Tuple{Int64, String}}
1003+
@NamedTuple{a::Int64, b::String}
9991004
```
10001005

10011006
A `NamedTuple` type can be used as a constructor, accepting a single tuple argument.
10021007
The constructed `NamedTuple` type can be either a concrete type, with both parameters specified,
10031008
or a type that specifies only field names:
10041009

10051010
```jldoctest
1006-
julia> @NamedTuple{a::Float32,b::String}((1,""))
1011+
julia> @NamedTuple{a::Float32,b::String}((1, ""))
10071012
(a = 1.0f0, b = "")
10081013
1009-
julia> NamedTuple{(:a, :b)}((1,""))
1014+
julia> NamedTuple{(:a, :b)}((1, ""))
10101015
(a = 1, b = "")
10111016
```
10121017

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)