question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[io-2] Conceptual design for IO format

See original GitHub issue

Here 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:open
  • Created 4 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
fvascocommented, Oct 28, 2019

Hi @altavir, the readThis() and writeThis(obj) functions look confusing to me, I propose something like readObject and writeObject, similar to readInt and writeInt.

Moreover I don’t understand why IOFormat interface contains only extension methods, do you wish to use it as an implicit parameter?

with (output) {
 with (UserFormat) {
  with (BookFormat) {
   write (user)
   write (book)
  }
 }
}

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).

0reactions
fvascocommented, Oct 28, 2019

I’ve replaced writeThis by writeObject. It looks more concise.

Please think twice about motivation. this has a special meaning in Kotlin.

maybe two small extensions for lists and maps

So my disapproval is still valid.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found