-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
iterator: improve type inference for Filter
#59142
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
Conversation
3c99fd0 to
664a71d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK, it is permitted for any two-element struct to be returned (such as NamedTuple or Pair), as long as it implements indexing corresponding to fields
Okay, then I guess we probably need to update the docstring for |
|
whether or not the inner iterator uses or is allowed to use a |
|
But even if |
|
Heh, within a second :) |
|
Indeed, from the view point of |
|
Actually I think the previous change may be better. When iterators don't return tuples, ther s often a reason, e.g. to avoid excessive specialization. While I agree it is legal to return the tuple, I'm not sure it's useful. |
|
That's certainly true. There's a possibility that other implementations of |
This addresses type inference issues with filtered list comprehensions reported in #59111. The root cause is the compiler's inability to perform type refinement in cases like: ```julia let t::Tuple{Any,Any} x, y = t if x isa Int return t # still `::Tuple{Any,Any}` end nothing end ``` Fixing this fundamentally would require extensive improvements to abstract interpretation. As a workaround, I improved the iterators.jl implementation to avoid this pattern. One implementation consideration: if `iterate(f.itr, state...)` always returns `Union{Nothing,Tuple{Any,Any}}`, the `if y isa Tuple{Any,Any}` branch should be unnecessary. However, it's unclear whether we can safely assume this for the iterate interface, so I kept the branch for now.
This reverts commit de8ec5d.
a8eade2 to
45e6823
Compare
|
Is there an actual example of an iterator that returns |
|
There are lots of cases. You can easily write such a custom iterate implementation while satisfying the iterator protocol. |
This addresses type inference issues with filtered list comprehensions reported in #59111. The root cause is the compiler's inability to perform type refinement in cases like:
Fixing this fundamentally would require extensive improvements to
abstract interpretation. As a workaround, I improved the iterators.jl
implementation to avoid this pattern.
One implementation consideration: if
iterate(f.itr, state...)alwaysreturns
Union{Nothing,Tuple{Any,Any}}, theif y isa Tuple{Any,Any}branch should be unnecessary. However, it's unclear whether we can
safely assume this for the iterate interface, so I kept the branch for
now.