Closed witoldsz closed 4 years ago
@witoldsz That is expected behavior of a stream once you hit a terminal operation such as forEach
you have consumed the stream already.
Simple pure Java example
IntStream stream = IntStream.of();
stream.forEach(System.out::println);
stream.forEach(System.out::println); // java.lang.IllegalStateException: stream has already been operated upon or closed
Well, you are right. I was using Seq as immutable List, since Java introduced immutable lists, but did not bother to add operations for them :-/
Will work this out, though. Thanks!
Well, this is surprising:
@Test
public void test() {
var x = Seq.empty();
System.out.println(x);
x.forEach(System.out::println);
System.out.println(x);
x.append(3); // <-- no exception here :O
}
x.append(3); // <-- no exception here :O
Well, we don't guarantee this exception. Should we?
Well, we don't guarantee this exception. Should we?
Well, no need to :)
It was just unexpected for me, because my code was (kind of) OK until I've removed one System.out.println
and it started to crash :O
Oh my, it is SO PAINFUL to be back in Java world again… (not for long, hopefully), but it's so much easier with jOOQ/jOOL!
From the spec:
A stream implementation may throw IllegalStateException if it detects that the stream is being reused. However, since some stream operations may return their receiver rather than a new stream object, it may not be possible to detect reuse in all cases
Thanks, @amaembo. I was afraid I had new work, here! :)
@witoldsz. It may be painful, but it will be a joy discussing things with you again 😁
I HAVE TO admit that the Seq.empty()
might become a surprise quite often, when it's used as a seed, identity or default value in many situations.
The trouble is, when using things like Collections.emptyList()
(or it's newer cousin List.of()
), Optional.empty()
, etc… you can easily misuse Seq.empty()
as an immutable seed value, but Seq.empty()
produces a mutable beast even though most of the operations on it return new Seq. It IS LOGICAL, because it's a stream after all… and Stream.empty()
can cause you similar troubles.
Wonderful world of "pure" OOP hidden complexity.
Steps to reproduce the problem:
Versions:
--enable-preview
but it shouldn't matter, I guess)