Skip to content

show_default, but without type parameters #36263

@goretkin

Description

@goretkin

Definitions like

show(io::IO, r::OneTo) = print(io, "Base.OneTo(", r.stop, ")")
could be no more than something like

show(io::IO, r::OneTo) = show_default(io, r; type_parameters=false)

There are many places where that's a default behavior that is wanted. For example, when all the type parameters correspond to fields that when show'd reveal their type. For example

struct Foo{T}
  x::T
end

show(Foo(Base.OneTo(3)))

produces

Foo{Base.OneTo{Int64}}(Base.OneTo(3)),
which is unnecessary.

Also, show_default has great logic to use IOContext to see which names need to be qualified or not:

julia> Base.show_default(stdout, Base.OneTo(3))
Base.OneTo{Int64}(3)
julia> Base.show_default(stdout, Foo(Base.OneTo(3)))
Foo{Base.OneTo{Int64}}(Base.OneTo(3))
julia> using Base: OneTo

julia> Base.show_default(stdout, Base.OneTo(3))
OneTo{Int64}(3)
julia> Base.show_default(stdout, Foo(Base.OneTo(3)))
Foo{OneTo{Int64}}(Base.OneTo(3))

Notice the inconsistency in Foo{OneTo{Int64}}(Base.OneTo(3)).

x-ref: #33260

Metadata

Metadata

Assignees

No one assigned

    Labels

    display and printingAesthetics and correctness of printed representations of objects.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions