-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Abstract iterator type? #23429
Comments
Iterators in Julia are usually duck-typed, with traits to get the eltype. Because iterability is such a common thing to implement, if we tried to use an abstract type for this then then the lack of multiple inheritance would probably bite us — there would be no way to make a type iterable if its supertype was not iterable. |
You'd also run into problems from the fact that numbers are iterable (#7903). As I understand it, it would not be valid in Julia to recursively declare |
Yeah, I don't think this would cover all situations, but I figured it might be nice when designing an API to have the option of dispatching on an iterator type rather than including checks in your methods. Maybe the lack of multiple inheritance could be addressed by a default |
If you dispatch on an iterator type, then you won't handle all iterators. What checks are you doing in your methods? Duck typing means that you just iterate, and let Julia throw a MethodError if Also, there is a distinction between the case where the
|
That seems fine as long as there's a way for someone to create an iterator type.
Yeah, I'd mostly like to exit early rather than waiting till I try to iterate. Currently, I'm just checking the |
I think this is really something to be handled via traits (or interfaces or multiple inheritance or something like that). Note that there is an |
I have a package for checking that something implements the the Iterator interface. Which is semi-orthogonal to this -- since the question is about dispatch not contract checking. Also the code in there shows what promises interfaces make, and how to check those. |
@davidanthoff: The |
@mauro3 Ah, yes, now I remember! Would be nice if that would work eventually :) |
Would there be any interest in adding an
AbstractIterator{T}
type in base? There seems to be a common pattern of defining an iterator type anyways (e.g.,KeyIterator
,ValueIterator
) and I think it would help with a lot of design issues within the package ecosystem if we could just accept anAbstractIterator{T}
whereT
is some commoneltype
we want to support.The text was updated successfully, but these errors were encountered: