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

Add an optional "at stop" method to the iterator interface #17954

Closed
aaronsheldon opened this issue Aug 10, 2016 · 2 comments
Closed

Add an optional "at stop" method to the iterator interface #17954

aaronsheldon opened this issue Aug 10, 2016 · 2 comments

Comments

@aaronsheldon
Copy link

aaronsheldon commented Aug 10, 2016

Proposal

The iterator interface is a powerful mechanism for encapsulating lazy extraction from an external data source. In its current form there is an asymmetry between when server side resources can be requested (in "start"), and when server side resources can be released (manually after loop completion). To address this an optional "stop method" could be added that is automatically run on all forms of loop exit.

In particular putting an iterable reader of an external resource into a comprehension requires following that comprehension with some form of signal to the external data source to release server side resources.

The chief concern is that the caller of the iterable interface has no knowledge of any external resources that would have been requested during "start", and would find those variables to be inaccessible at the end of iteration (at least without making a potentially error generating call to "next").

Example Use Case

Consider a type that encapsulates an iterable connection to a data source (e.g. the classic forward only data reader)

immutable DataReader <: AbstractIterableConnections
    ...
end

To implement the iterations we would overload in the following manner

start(x::DataReader)
    ...
    state = newservercursor(DataReader)
    ...
    state
end
next(x::DataReader, s::cursor)
    ...
    y = read!(s)
    ...
    (y, s)
end
done(x::DataReader, s::cursor) = x.closed || s.eof
stop(x::DataReader, s::cursor)
    ...
    releaseresources!(s)
    ...
end

Where the default definition of stop is given by

stop(::Any, ::Any) = nothing
@stevengj
Copy link
Member

See also #11207.

@mauro3
Copy link
Contributor

mauro3 commented Aug 11, 2016

x-ref #8149

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

No branches or pull requests

3 participants