ilaborie / Effekts

User defined effects for Kotlin multiplatform

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Effekts

User defined effects for Kotlin multiplatform

Example

User defined effects

sealed class IOConsole<T> : Effect<T>() {
    data class printString(val text: String) : IOConsole<Unit>()
    class readString : IOConsole<String>()
}

DSL for effects handling

handle<Unit, IOConsole<*>> {
    val name: String = perform(readString())
    perform(printString("Hello $name"))
} with {
    when (it) {
        is printString -> {
            println(it.text)
            it.resume(Unit)
        }
        is readString -> {            
            it.resume("World")
        }
    }
}

About

User defined effects for Kotlin multiplatform

License:Apache License 2.0


Languages

Language:Kotlin 100.0%