Possible JSR-310 (date/time API) stream methods
Brian Goetz
brian.goetz at oracle.com
Fri Mar 8 08:39:07 PST 2013
Actually I was talking about:
Iterable ii = () -> iterator
which is dangerous.
On 3/8/2013 11:31 AM, Zhong Yu wrote:
> 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