[io-2] Conceptual design for IO format
See original GitHub issueHere are some thoughts about IO format functionality I would like to add as soon as IO-2 is out. The idea is that we can add an easy way to write objects to streams and read them from streams. The resulting API could then simplify the work with serialization and file IO.
The idea is shown in the following example (using io-1 API):
interface IOFormat<T : Any> {
fun Output.writeThis(obj: T)
fun Input.readThis(): T
}
fun <T : Any> Input.readWith(format: IOFormat<T>): T = format.run { readThis() }
fun <T : Any> Output.readWith(format: IOFormat<T>, obj: T) = format.run { writeThis(obj) }
class ListIOFormat<T : Any>(val format: IOFormat<T>) : IOFormat<List<T>> {
override fun Output.writeThis(obj: List<T>) {
writeInt(obj.size)
format.run {
obj.forEach {
writeThis(it)
}
}
}
override fun Input.readThis(): List<T> {
val size = readInt()
return format.run {
List(size) { readThis() }
}
}
}
val <T: Any> IOFormat<T>.list get() = ListIOFormat(this)
IOFormat
represents a way to read and write data with Input and Output. One can construct a format for complex objects from the formats of individual parts. It could be probably used as a way to customize serialization backends. The similar idea is currently used in kmath in-memory operations on data: https://github.com/mipt-npm/kmath/blob/dev/kmath-memory/src/commonMain/kotlin/scientifik/memory/MemorySpec.kt.
Issue Analytics
- State:
- Created 4 years ago
- Comments:7
Top Results From Across the Web
Introduction to XtremIO X2 Storage Array - Dell
This white paper introduces the Dell EMC XtremIO X2 Storage Array (with. XIOS Version 6.3.0). It provides detailed descriptions of the system.
Read more >Amazon RDS DB instance storage - AWS Documentation
Work with the different storage types available for a DB instance on Amazon RDS.
Read more >Conceptual Design, Implementation, and Evaluation of ... - NCBI
Objectives The objective of this study is the conceptual design, implementation and evaluation of a system for generic, standard-compliant ...
Read more >Generation of I-O Formats - IBM
When all field descriptions are identical, and you have requested INPUT or OUTPUT fields implicitly or explicitly, only one set of field descriptions...
Read more >Conceptual Design Environment | Revit 2019
Use the conceptual design environment to create a conceptual mass or a pattern component. Select a template to provide a starting point.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi @altavir, the
readThis()
andwriteThis(obj)
functions look confusing to me, I propose something likereadObject
andwriteObject
, similar toreadInt
andwriteInt
.Moreover I don’t understand why
IOFormat
interface contains only extension methods, do you wish to use it as an implicit parameter?In closing, I wish understand why should this feature be included in this library, serialization libraries already perform this task (see kotlinx.serialization as example).
Please think twice about motivation.
this
has a special meaning in Kotlin.So my disapproval is still valid.