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

Rename IndexValue -> Pairs, print it better #25764

Merged
merged 1 commit into from
Feb 2, 2018
Merged

Conversation

Keno
Copy link
Member

@Keno Keno commented Jan 26, 2018

As discussed in #25711

Before:

julia> f(;kwargs...) = kwargs
f (generic function with 1 method)

julia> f(;a = 1, b = 2)
Base.Iterators.IndexValue{Symbol,Int64,Tuple{Symbol,Symbol},NamedTuple{(:a, :b),Tuple{Int64,Int64}}} with 2 entries:
  :a => 1
  :b => 2

After:

julia> f(;a = 1, b = 2)
pairs((a = 1, b = 2)) with 2 entries:
  :a => 1
  :b => 2

@ararslan ararslan added the domain:collections Data structures holding multiple items, e.g. sets label Jan 26, 2018
Copy link
Sponsor Member

@vtjnash vtjnash left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we perhaps calls this a PairsView?


# forward declarations for use by iterators.jl (since parts of that file
# are used by the compiler)
function showarg end
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

editor forgetting to add newlines?

print(io, "pairs(", r.data, ")")
else
print(io, "Iterators.Pairs(",r.data, ", ", r.itr,")")
end
end
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we usually put these definitions in show.jl to avoid the ordering issue. Also, missing a few spaces after the commas

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can do.

end

function Base.showarg(io::IO, r::Pairs, toplevel)
if try typeof(pairs(r.data)) == typeof(r); catch; false; end
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is there a try/catch trying to hide on this line?

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a bad way to handle this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seemed like pairs might throw?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, this is an interactive-use function, so I didn't think it was too-terrible. The query is basically "Is this the type I'd get if I called pairs on this thing". Seemed like the most straightforward way to express that. We could also do something with return_types, but not sure that's better.

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a great reply typed out, then clicked the wrong button. C'est la vie, I suppose – second draft is always better, right?

I looked at how the other showarg functions are implemented, and noticed / realized that none of them care about whether the printed syntax is precise, just whether it is useful. That is to say, they only care if its a reasonable interpretation. None of them care to check if that interpretation is precise.

In conclusion, the implementation should simply be:

print(io, "pairs(")
showarg(io, r.data, false)
print(io, ")")

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And also define:

showarg(io, nt::NamedTuple, toplevel) = print(io, toplevel ? typeof(nt) : "::NamedTuple")
showarg(io, tt::Tuple, toplevel) = print(io, toplevel ? typeof(tt) : "::Tuple")

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In conclusion, the implementation should simply be:

Well, The Pairs iterator allows different index sets, so e.g. pairs(IndexCartesian(), A) should not print as pairs(A).

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the same could be said of the other showarg functions. Mostly, they'll be distinguished by the printed data. But we can just extend this to handle that case also.

Copy link
Member Author

@Keno Keno Jan 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. If we print a simple pairs( line, we should make sure it's correct. We wouldn't print ReshapedArray as reshape(::Array, rand(Int), rand(Int)).

@Keno Keno added this to the 1.0 milestone Feb 1, 2018
As discussed in #25711

Before:
```
julia> f(;kwargs...) = kwargs
f (generic function with 1 method)

julia> f(;a = 1, b = 2)
Base.Iterators.IndexValue{Symbol,Int64,Tuple{Symbol,Symbol},NamedTuple{(:a, :b),Tuple{Int64,Int64}}} with 2 entries:
  :a => 1
  :b => 2
```

After:
```
julia> f(;a = 1, b = 2)
pairs((a = 1, b = 2)) with 2 entries:
  :a => 1
  :b => 2
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:collections Data structures holding multiple items, e.g. sets
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants