Sequences are most useful for large data sets, and these datasets are in many cases originated in some kind of a resource like a database cursor or a file. So the underlying resources remains open until the sequence is processed. But if an exception is thrown during processing, the resource might remain open, because the Kotlin
Sequence doesn’t implement
AutoCloseable, so if you use the
asStream conversion method from the Kotlin standard library, you lose this advantage!
A concrete example is the
getResultStream method from JPA. If this method is used, the underlying resource ( a cursor in the database and the Java object representing it) remains open in case of an exception.
Was it simply “forgotten” to implement
Sequence or is there any reasoning behind it?
Is there a best-practice to work around this limitation? The best that comes to mind is to create an own sequence wrapper type that implements