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

how to handle rest with invalid state argument? #15440

Open
mschauer opened this issue Mar 10, 2016 · 12 comments
Open

how to handle rest with invalid state argument? #15440

mschauer opened this issue Mar 10, 2016 · 12 comments
Labels
design Design of APIs or of the language itself

Comments

@mschauer
Copy link
Contributor

As the rest of an iterator can contain more elements then the original one, e.g.

collect(rest(1:5,0))

maybe from(iter, state) would be a better name.

@nalimilan
Copy link
Member

Probably better use a name which is common in other languages. Would you make a short survey?

@yuyichao
Copy link
Contributor

Shouldn't iterating on rest(1:5, 0) raise a bounds error?

@mschauer
Copy link
Contributor Author

@nalimilan I think this might be julia specific, python and java do not allow this and mandate the use of tee and others only "restart" at the "beginning" but that is not terribly well defined.

@yuyichao This behaviour is only possible in specific cases. In general you cannot test if a certain state is ever reached from an iterator.

@toivoh
Copy link
Contributor

toivoh commented Mar 10, 2016

Isn't the interpretation of the iterator state meant to be an entirely private matter to the type being iterated over? This function strikes me as dangerous, as it encourages people to make assumptions about such implementation details, which might actually change at any time. Perhaps there are good reasons that make it needed in Base, but instead of renaming it I suggest we stop exporting it.

@yuyichao
Copy link
Contributor

I can see this being useful when you are calling start, next etc yourself. I don't think supplying a random state should be supported for exactly the reason @toivoh mentioned though.

@JeffBezanson
Copy link
Member

Yes, we're assuming the state is valid. It's definitely a general issue, e.g. UTF-8 string indexes come to mind. I don't see anything we can do about it without a performance disaster.

@JeffBezanson
Copy link
Member

However, we could have a function to check whether a state is valid, and call it when constructing a Rest iterator.

@toivoh
Copy link
Contributor

toivoh commented Mar 10, 2016

But would it be a performance disaster to limit the usage to Base? I doubt that a lot of code outside of Base is using it anyway.

@JeffBezanson JeffBezanson changed the title rest of an iterator is a misnomer how to handle rest with invalid state argument? Mar 11, 2016
@JeffBezanson JeffBezanson added the needs decision A decision on this change is needed label Mar 11, 2016
@mschauer
Copy link
Contributor Author

Datapoint: In @samoconnor called the same functionality in #14805 "resume"

@samoconnor
Copy link
Contributor

Isn't the interpretation of the iterator state meant to be an entirely private matter to the type being iterated over?

Yes. state should be treated as an opaque token.

This function strikes me as dangerous, as it encourages people to make assumptions about such implementation details, which might actually change at any time.

The function isn't dangerous. Passing hand constructed state values is dangerous. But surely there are lots of functions that do unpredictable things when passed random values.
It seems to me that rest(x, state) is no different to done(x, state) and next(x, state).
The only valid state to pass to any of these is a value returned by start or next.

Is there anything in the documentation to suggest that constructing a state object by hand is an ok thing to do?

@mschauer
Copy link
Contributor Author

Yes, and I like a name derived from "resume" because that conveys the notion better that the argument is a state which was obtained earlier using legal means.

@StefanKarpinski StefanKarpinski added design Design of APIs or of the language itself and removed needs decision A decision on this change is needed labels Sep 13, 2016
@StefanKarpinski StefanKarpinski added this to the 0.6.0 milestone Sep 13, 2016
@JeffBezanson
Copy link
Member

There doesn't seem to be enough clarity on what (if anything) to do here for this to make 0.6.

@JeffBezanson JeffBezanson modified the milestones: 1.0, 0.6.0 Dec 28, 2016
@JeffBezanson JeffBezanson modified the milestones: 2.0+, 1.0 May 10, 2017
@StefanKarpinski StefanKarpinski modified the milestones: 2.0, 1.x Jan 22, 2019
@DilumAluthge DilumAluthge removed this from the 1.x milestone Mar 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design Design of APIs or of the language itself
Projects
None yet
Development

No branches or pull requests

8 participants