Possible JSR-310 (date/time API) stream methods

Zhong Yu zhong.j.yu at gmail.com
Fri Mar 8 08:31:04 PST 2013


On Fri, Mar 8, 2013 at 10:04 AM, Brian Goetz <brian.goetz at oracle.com> wrote:
>> If a user needs an Iterable, it's not too much trouble either
>>
>>      Iterable<A> iterable = ()->source.stream().iterator();
>
>
> While this satisfies the type system, it is asking for trouble.  Users
> routinely assume Iterable means "can be iterated repeatedly."  This is a
> pretty crippled Iterable.

You are probably talking about

    Iterable<A> iterable = ()->stream.iterator();

which is indeed dangerous.

But in my example, every `Iterable.iterator()` asks for a new stream -
`source.stream()`, that should be OK. Unless, source.stream() is also
not well defined and we don't know if it creates a new stream or
returns the same stream.

Zhong Yu

>
>
>> That should cover all legacy usages that interface with
>> Iterable/Iterator. So I agree that new APIs can forget about
>> Iterable/Iterator.
>
>
> Still, I agree with this conclusion.  Returning Iterator/Iterable as a LCD
> is nearly always the wrong thing.
>
> If you know the caller needs a reified aggregate, return a Collection. If
> you think the caller merely needs access to the elements, return a Stream,
> with the best Spliterator you can give it.


More information about the lambda-dev mailing list