Remi Forax forax at
Tue Jun 25 06:21:28 PDT 2013

On 06/25/2013 01:07 PM, Paul Sandoz wrote:
> What about the following:
>    Stream s = Files.lines(path));
>    s = s.filter(goGetMeAPredicate()); // Exception thrown
>    return s.collect(toList());
>    ...
>    Predicate goGetMeAPredicate() {
>      throw new RuntimeException();
>    }
> There may be other stuff being performed in conjunction with the stream holding a GCR stuff.
> Are you proposing that GCR state should be lazily initialized when the terminal operation commences with appropriate management of that state in the AutoCloseable?

yes, so it's not an AutoClosable but a supplier of AutoClosable.

> That would also result in errors at a distance.

first, initializing the resource early don't fit well with the stream 
model of lazy/terminal methods
and anyway calling close() also result in an error at the same time.

To summarize, either the user create explicitly a Reader (a resource) 
and in that case it has to use a TWR,
or it doesn't want to create the resource explicitly (Files.lines is a 
good example) and
the resource is created and closed when doing the terminal operation.

> Paul.


> On Jun 25, 2013, at 9:30 AM, Remi Forax <forax at> wrote:
>> As someone said one the lambda-dev mailing list, there is no TWR in C# because close is called at the end of the for-each instruction.
>> Why do you want user to care about releasing resources manually if this can be done automatically ?
>> A Stream is an abstraction over a loopy thing, so like in C#, the Stream should call close at the end if the source is an AutoCloseable.
>> Thus either the Spliterator takes care of the resource, but conceptually a Spliterator is more an abstraction over a traversal algorithm
>> or the Stream takes care of the resource.
>> Thus the solution is easy, instead of constructing a Stream with 2 parameters, the supplier of spliterator and the characteristics,
>> we need a third parameter, an AutoCloseable that may be null and in the implementation of all terminal methods of Stream,
>> we have to enclose the 'loop' over the spliterator inside a TWR.
>> Sometimes the simple solution is the best one.
>> Rémi

More information about the lambda-libs-spec-experts mailing list