Skip to content

Commit 6901610

Browse files
JeffBezansonpull[bot]
authored andcommitted
add tuple type printing with Vararg, and port to static_show (JuliaLang#44970)
1 parent 5656dc0 commit 6901610

File tree

3 files changed

+53
-13
lines changed

3 files changed

+53
-13
lines changed

base/show.jl

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -993,19 +993,31 @@ function show_datatype(io::IO, x::DataType, wheres::Vector{TypeVar}=TypeVar[])
993993
istuple = x.name === Tuple.name
994994
n = length(parameters)
995995

996-
# Print homogeneous tuples with more than 3 elements compactly as NTuple{N, T}
996+
# Print tuple types with homogeneous tails longer than max_n compactly using `NTuple` or `Vararg`
997+
max_n = 3
997998
if istuple
998-
if n > 3 && all(@nospecialize(i) -> (parameters[1] === i), parameters)
999+
taillen = 1
1000+
for i in (n-1):-1:1
1001+
if parameters[i] === parameters[n]
1002+
taillen += 1
1003+
else
1004+
break
1005+
end
1006+
end
1007+
if n == taillen > max_n
9991008
print(io, "NTuple{", n, ", ")
10001009
show(io, parameters[1])
10011010
print(io, "}")
10021011
else
10031012
print(io, "Tuple{")
1004-
# join(io, params, ", ") params but `show` it
1005-
first = true
1006-
for param in parameters
1007-
first ? (first = false) : print(io, ", ")
1008-
show(io, param)
1013+
for i = 1:(taillen > max_n ? n-taillen : n)
1014+
i > 1 && print(io, ", ")
1015+
show(io, parameters[i])
1016+
end
1017+
if taillen > max_n
1018+
print(io, ", Vararg{")
1019+
show(io, parameters[n])
1020+
print(io, ", ", taillen, "}")
10091021
end
10101022
print(io, "}")
10111023
end

src/rtutils.c

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,37 @@ static size_t jl_static_show_x_(JL_STREAM *out, jl_value_t *v, jl_datatype_t *vt
785785
jl_sym_t *sym = globfunc ? globname : dv->name->name;
786786
char *sn = jl_symbol_name(sym);
787787
size_t quote = 0;
788+
if (dv->name == jl_tuple_typename) {
789+
if (dv == jl_tuple_type)
790+
return jl_printf(out, "Tuple");
791+
int taillen = 1, tlen = jl_nparams(dv), i;
792+
for (i = tlen-2; i >= 0; i--) {
793+
if (jl_tparam(dv, i) == jl_tparam(dv, tlen-1))
794+
taillen++;
795+
else
796+
break;
797+
}
798+
if (taillen == tlen && taillen > 3) {
799+
n += jl_printf(out, "NTuple{%d, ", tlen);
800+
n += jl_static_show_x(out, jl_tparam0(dv), depth);
801+
n += jl_printf(out, "}");
802+
}
803+
else {
804+
n += jl_printf(out, "Tuple{");
805+
for (i = 0; i < (taillen > 3 ? tlen-taillen : tlen); i++) {
806+
if (i > 0)
807+
n += jl_printf(out, ", ");
808+
n += jl_static_show_x(out, jl_tparam(dv, i), depth);
809+
}
810+
if (taillen > 3) {
811+
n += jl_printf(out, ", Vararg{");
812+
n += jl_static_show_x(out, jl_tparam(dv, tlen-1), depth);
813+
n += jl_printf(out, ", %d}", taillen);
814+
}
815+
n += jl_printf(out, "}");
816+
}
817+
return n;
818+
}
788819
if (globfunc) {
789820
n += jl_printf(out, "typeof(");
790821
}
@@ -804,9 +835,7 @@ static size_t jl_static_show_x_(JL_STREAM *out, jl_value_t *v, jl_datatype_t *vt
804835
n += jl_printf(out, ")");
805836
}
806837
}
807-
if (dv->parameters && (jl_value_t*)dv != dv->name->wrapper &&
808-
(jl_has_free_typevars(v) ||
809-
(jl_value_t*)dv != (jl_value_t*)jl_tuple_type)) {
838+
if (dv->parameters && (jl_value_t*)dv != dv->name->wrapper) {
810839
size_t j, tlen = jl_nparams(dv);
811840
if (tlen > 0) {
812841
n += jl_printf(out, "{");
@@ -818,9 +847,6 @@ static size_t jl_static_show_x_(JL_STREAM *out, jl_value_t *v, jl_datatype_t *vt
818847
}
819848
n += jl_printf(out, "}");
820849
}
821-
else if (dv->name == jl_tuple_typename) {
822-
n += jl_printf(out, "{}");
823-
}
824850
}
825851
}
826852
else if (vt == jl_intrinsic_type) {

test/show.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,6 +1338,8 @@ test_repr("(:).a")
13381338
@test repr(NTuple{7,Int64}) == "NTuple{7, Int64}"
13391339
@test repr(Tuple{Float64, Float64, Float64, Float64}) == "NTuple{4, Float64}"
13401340
@test repr(Tuple{Float32, Float32, Float32}) == "Tuple{Float32, Float32, Float32}"
1341+
@test repr(Tuple{String, Int64, Int64, Int64}) == "Tuple{String, Int64, Int64, Int64}"
1342+
@test repr(Tuple{String, Int64, Int64, Int64, Int64}) == "Tuple{String, Vararg{Int64, 4}}"
13411343

13421344
@testset "issue #42931" begin
13431345
@test repr(NTuple{4, :A}) == "NTuple{4, :A}"

0 commit comments

Comments
 (0)