colomboe / KIO

A simple, lightweight IO monad implementation for Kotlin with effect rotation
https://colomboe.github.io/KIO/
Apache License 2.0
59 stars 4 forks source link

R as context receiver for functions #9

Open rascio opened 1 year ago

rascio commented 1 year ago

Hi there! As first thing very nice project for who like me would like to move to ZIO but is stuck with Kotlin 😛

I was playing around with it and not sure if I wasn't unable to find it or there is a reason to not have the R parameter of the KIO in the functions for flatMap/map

Something like:

inline fun <R, E, A, O> KIO<R, E, A>.flatMapC(crossinline fn: R.(A) -> KIO<R, E, O>) = flatMap { value ->
    askPure<R>().flatMap { ctx -> fn(ctx, value) }
}

so that one can do:

data class Context(val messageId: String)
val program: KIO<Context, Throwable, String> = TODO()

program.flatMapC { value -> println("messageId: $messageId") }

I couldn't find something similar, but seems a nice way of accessing it taking advantage of Kotlin features

colomboe commented 1 year ago

IIRC, there are two downsides in having always injected the R in the context:

Anyway I think this could be a good addition to the already available methods set. Something like flatMapR or askFlatMap or something like this.