Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

methodshow: single-line printing for Method by default #46241

Merged
merged 2 commits into from
Aug 6, 2022
Merged

Conversation

aviatesk
Copy link
Sponsor Member

@aviatesk aviatesk commented Aug 3, 2022

Currently show(::IO, ::Method) prints the method object in
multiple-line as like:

julia> only(methods(sin, (Float64,)))
sin(x::T) where T<:Union{Float32, Float64}
     @ Base.Math special/trig.jl:29

and this could be confusing when used within a container e.g.:

julia> Any[only(methods(sin, (Float64,)))]
1-element Vector{Any}:
 sin(x::T) where T<:Union{Float32, Float64}
     @ Base.Math special/trig.jl:29

julia> code_lowered() do; Base.Experimental.@opaque a::Int -> sin(a); end
1-element Vector{Core.CodeInfo}:
 CodeInfo(
1%1 = Core.apply_type(Core.Tuple, Main.Int)
│   %2 = Core.apply_type(Core.Union)
│   %3 = $(Expr(:new_opaque_closure, :(%1), :(%2), :(Core.Any), opaque closure(...)
     @ Main none:0))
└──      return %3
)

This commit refactors the show method for Method object so that
show(::IO, ::Method) prints it within a single line by default and
show(::IO, ::MIME"text/plain", ::Method) prints within 2-lines,
that I believe is more aligned with printing implementations for
the other types:

julia> Any[only(methods(sin, (Float64,)))]
1-element Vector{Any}:
 sin(x::T) where T<:Union{Float32, Float64} @ Base.Math special/trig.jl:29

julia> code_lowered() do; Base.Experimental.@opaque a::Int -> sin(a); end
1-element Vector{Core.CodeInfo}:
 CodeInfo(
1%1 = Core.apply_type(Core.Tuple, Main.Int)
│   %2 = Core.apply_type(Core.Union)
│   %3 = $(Expr(:new_opaque_closure, :(%1), :(%2), :(Core.Any), opaque closure(...) @ Main none:0))
└──      return %3
)

/cc @Seelengrab

@Seelengrab
Copy link
Contributor

Does this impact printing of e.g. methods(sin) or of MethodError? If not, fixing the generic Any container case and the code_* cases seems like a good idea 👍

@aviatesk
Copy link
Sponsor Member Author

aviatesk commented Aug 4, 2022

Does this impact printing of e.g. methods(sin) or of MethodError?

No, those printings won't be affected by this PR>

Currently `show(::IO, ::Method)` prints the method object in
multiple-line as like:
```julia
julia> only(methods(sin, (Float64,)))
sin(x::T) where T<:Union{Float32, Float64}
     @ Base.Math special/trig.jl:29
```
and this could be confusing when used within a container e.g.:
```julia
julia> Any[only(methods(sin, (Float64,)))]
1-element Vector{Any}:
 sin(x::T) where T<:Union{Float32, Float64}
     @ Base.Math special/trig.jl:29

julia> code_lowered() do; Base.Experimental.@opaque a::Int -> sin(a); end
1-element Vector{Core.CodeInfo}:
 CodeInfo(
1 ─ %1 = Core.apply_type(Core.Tuple, Main.Int)
│   %2 = Core.apply_type(Core.Union)
│   %3 = $(Expr(:new_opaque_closure, :(%1), :(%2), :(Core.Any), opaque closure(...)
     @ Main none:0))
└──      return %3
)
```

This commit refactors the `show` method for `Method` object so that
`show(::IO, ::Method)` prints it within a single line by default and
`show(::IO, ::MIME"text/plain", ::Method)` prints within 2-lines,
that I believe is more aligned with printing implementations for
the other types:
```julia
julia> Any[only(methods(sin, (Float64,)))]
1-element Vector{Any}:
 sin(x::T) where T<:Union{Float32, Float64} @ Base.Math special/trig.jl:29

julia> code_lowered() do; Base.Experimental.@opaque a::Int -> sin(a); end
1-element Vector{Core.CodeInfo}:
 CodeInfo(
1 ─ %1 = Core.apply_type(Core.Tuple, Main.Int)
│   %2 = Core.apply_type(Core.Union)
│   %3 = $(Expr(:new_opaque_closure, :(%1), :(%2), :(Core.Any), opaque closure(...) @ Main none:0))
└──      return %3
)
```
@aviatesk
Copy link
Sponsor Member Author

aviatesk commented Aug 4, 2022

CI looks fine. Are there any objections? Otherwise I'd merge this soonish.

@Liozou
Copy link
Member

Liozou commented Aug 5, 2022

Nice! I believe this incidentally fixes #45836 and supersedes #45844 by making REPL completion of methods revert to being one line each (which I find completely fine, but can be tweaked that in a later PR if need be).

@Seelengrab
Copy link
Contributor

Yeah, looks good to me! Thank you as well for cleaning up the indentation numbers being passed around :)

@aviatesk aviatesk merged commit 42c5f6c into master Aug 6, 2022
@aviatesk aviatesk deleted the avi/methodshow branch August 6, 2022 03:17
ffucci pushed a commit to ffucci/julia that referenced this pull request Aug 11, 2022
…6241)

Currently `show(::IO, ::Method)` prints the method object in
multiple-line as like:
```julia
julia> only(methods(sin, (Float64,)))
sin(x::T) where T<:Union{Float32, Float64}
     @ Base.Math special/trig.jl:29
```
and this could be confusing when used within a container e.g.:
```julia
julia> Any[only(methods(sin, (Float64,)))]
1-element Vector{Any}:
 sin(x::T) where T<:Union{Float32, Float64}
     @ Base.Math special/trig.jl:29

julia> code_lowered() do; Base.Experimental.@opaque a::Int -> sin(a); end
1-element Vector{Core.CodeInfo}:
 CodeInfo(
1 ─ %1 = Core.apply_type(Core.Tuple, Main.Int)
│   %2 = Core.apply_type(Core.Union)
│   %3 = $(Expr(:new_opaque_closure, :(%1), :(%2), :(Core.Any), opaque closure(...)
     @ Main none:0))
└──      return %3
)
```

This commit refactors the `show` method for `Method` object so that
`show(::IO, ::Method)` prints it within a single line by default and
`show(::IO, ::MIME"text/plain", ::Method)` prints within 2-lines,
that I believe is more aligned with printing implementations for
the other types:
```julia
julia> Any[only(methods(sin, (Float64,)))]
1-element Vector{Any}:
 sin(x::T) where T<:Union{Float32, Float64} @ Base.Math special/trig.jl:29

julia> code_lowered() do; Base.Experimental.@opaque a::Int -> sin(a); end
1-element Vector{Core.CodeInfo}:
 CodeInfo(
1 ─ %1 = Core.apply_type(Core.Tuple, Main.Int)
│   %2 = Core.apply_type(Core.Union)
│   %3 = $(Expr(:new_opaque_closure, :(%1), :(%2), :(Core.Any), opaque closure(...) @ Main none:0))
└──      return %3
)
```
pcjentsch pushed a commit to pcjentsch/julia that referenced this pull request Aug 18, 2022
…6241)

Currently `show(::IO, ::Method)` prints the method object in
multiple-line as like:
```julia
julia> only(methods(sin, (Float64,)))
sin(x::T) where T<:Union{Float32, Float64}
     @ Base.Math special/trig.jl:29
```
and this could be confusing when used within a container e.g.:
```julia
julia> Any[only(methods(sin, (Float64,)))]
1-element Vector{Any}:
 sin(x::T) where T<:Union{Float32, Float64}
     @ Base.Math special/trig.jl:29

julia> code_lowered() do; Base.Experimental.@opaque a::Int -> sin(a); end
1-element Vector{Core.CodeInfo}:
 CodeInfo(
1 ─ %1 = Core.apply_type(Core.Tuple, Main.Int)
│   %2 = Core.apply_type(Core.Union)
│   %3 = $(Expr(:new_opaque_closure, :(%1), :(%2), :(Core.Any), opaque closure(...)
     @ Main none:0))
└──      return %3
)
```

This commit refactors the `show` method for `Method` object so that
`show(::IO, ::Method)` prints it within a single line by default and
`show(::IO, ::MIME"text/plain", ::Method)` prints within 2-lines,
that I believe is more aligned with printing implementations for
the other types:
```julia
julia> Any[only(methods(sin, (Float64,)))]
1-element Vector{Any}:
 sin(x::T) where T<:Union{Float32, Float64} @ Base.Math special/trig.jl:29

julia> code_lowered() do; Base.Experimental.@opaque a::Int -> sin(a); end
1-element Vector{Core.CodeInfo}:
 CodeInfo(
1 ─ %1 = Core.apply_type(Core.Tuple, Main.Int)
│   %2 = Core.apply_type(Core.Union)
│   %3 = $(Expr(:new_opaque_closure, :(%1), :(%2), :(Core.Any), opaque closure(...) @ Main none:0))
└──      return %3
)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants