last day (30 days later) » 

8:40 PM
room topic changed to Return Collection or Stream?: Discussion on question "Is it a good idea to substitute Collection for Stream in return values?" [java] [java-8] [java-stream]
4
Q: Is it a good idea to substitute Collection for Stream in return values?

VitaliyUp until Java 8, a property representing a collection of elements usually returned a Collection. At the absence of an immutable collection interface, a common idiom would be to wrap it as: Collection<Foo> getFoos(){ return Collections.unmodifiableCollection(foos); } Now that Stream is here, it...

1
A: Is it a good idea to substitute Collection for Stream in return values?

Stuart MarksLet me propose a simple rule: A Stream that is passed as a method argument or returned as a method's return value must be the tail of an unterminated pipeline. This is probably so obvious to those of us who have worked on streams that we never bothered to write it down. But it's probably no...

The rules you propose are absolutely sensible, the problem is that it increases mental burden. In the past, where everybody simply worked with Collections, developers never had to think twice before calling a method. They did not have to tread carefully around it. Moreover, it is often the case where given a sequence, it is convenient to traverse it more than once a few lines of code apart. Now, seemingly harmless operations suddenly become illegal, and for reasons that are not immediately apparent. This relates to our previous correspondence about the once-off nature of streams. (next comnt) — Vitaliy 2 hours ago
In a world where everybody is aware of this, and devs do not get tired, and always pay careful attention to what they write- this can be pulled off. But I feel that in our day to day reality- this is a recipe for annoying bugs, or even worse- emerging idioms that pass along a means to regenerate the stream at each point to achieve such functionality. Or even wrapper streams that simply cancel out the once off nature of streams, thus going against the language intentions. Sturat, is there a way we could converse in a more direct manner (email?), I feel this platform is too restricting.. — Vitaliy 2 hours ago
room topic changed to Return Collection or Stream?: Discussion on question "Is it a good idea to substitute Collection for Stream in return values?" [api-design] [java] [java-8] [java-stream]
I'm not sure I understand why this is more of a mental burden. If you think of a Stream as an Iterator on steroids, it's clear that it can be traversed once. If you need to traverse it multiple times, you pull it into your own collection.
Also, the burden on the method implementor is reduced. The implementor doesn't have to worry about wrapping a collection in an unmodifiable wrapper nor about making defensive copies.
I don't understand your point about wrapper streams canceling the one-off nature of streams.
 

  last day (30 days later) »