Hello. I am currently working on a from-scratch r...
# library-development
j
Hello. I am currently working on a from-scratch redesign of the kotlin-csv library, focusing primarily on the following: - Implementing lazy evaluation based on Sequence. - Supporting KMP by utilizing kotlinx-io for I/O operations. - Providing JVM-specific implementations as extensions limited to jvmMain. Please refer to the Pull Request for details, and I would appreciate your feedback on the public interface types and the overall design. https://kotlinlang.slack.com/archives/CMAL3470A/p1778030259345989
c
Have you considered
Flow
instead of
Sequence
? Sequences can't be closed, so they're often not very convenient for IO-related streaming. For example if you stream from a database, you need to close the cursor when you're done, and
Sequence
doesn't provide a way to do that.
👀 1
j
@CLOVIS Thank you. > Have you considered
Flow
instead of
Sequence
? To be honest, I wanted to build the core parts, such as the parser, using only the standard library, so I have not deeply considered whether to use Flow as an option. Your point that Flow is better suited for composition with files or DBIO, cancellation, and asynchronous processing is valid. On the other hand, as the original implementation was, the CsvReader and CsvWriter classes in this PR are designed as pure synchronous CSV converters that do not own I/O (excluding extension functions). As for the lifecycle of file handles, I have encapsulated them within a
read(path) { rows -> ... }
block API. However, considering the connectivity with DBIO and asynchronous streams, I thought it might be worth considering support for Flow.
Copy code
/**
 * Read CSV rows from [source] (UTF-8) and pass them to [block].
 * [source] is caller-owned — this function does not close it. The `Sequence`
 * passed to [block] must be consumed inside the block.
 */
fun <T> CsvReader.read(
    source: Source,
    options: CsvReadIoOptions = CsvReadIoOptions(),
    block: (Sequence<List<String>>) -> T,
): T = block(read(source.toCharSequence(options.stripBom)))