Hi, I am evaluating the `Effects and contexts` par...
# arrow
t
Hi, I am evaluating the
Effects and contexts
part of the documentation. Docs seem to indicate that
with
function can be used by multiple arguments
Copy code
suspend fun example() {
  db(connParams) { stdoutLogger {
    with(object : Database by this@db, Logger as this@stdoutLogger) {
      saveUserInDb(User("Alex"))
    }
  } }
}
However, the Kotlin std defines
with
with single argument
Copy code
public inline fun <T, R> with(receiver: T, block: T.() -> R): R {
Which
with
function should I use?
s
That uses the
with
with a single argument, but uses a special technique that allows for creating an anonymous object that implements multiple interfaces. It used to work at the time, not sure which Kotlin version. Is that not working anymore? It was discovered by @Alejandro Serrano Mena
t
Do you have sample code for that part? I can share my gist, but yeah it’s not compiling. It might be because not all functions are available in the sample and I tried to implement them myself.
k
Still working. You can do something like this:
Copy code
@Suppress("SUBTYPING_BETWEEN_CONTEXT_RECEIVERS")
inline fun <reified R, reified T1, reified T2, reified T3> with(
    t1: T1,
    t2: T2,
    t3: T3,
    block: context(T1, T2, T3) (TypeWrapper<T3>) -> R
): R {
    return block(t1, t2, t3, TypeWrapper.IMPL)
}

sealed interface TypeWrapper<out T> {
    object IMPL : TypeWrapper<Nothing>
}
s
Yes, that works but it's not what is happening here @Kristian Frøhlich. That examples predates context receivers
@Tonis Ives I don't have the full example on hand, I can try reproducing it later to see if it still works
k
Ah 👍
t
I’m sorry, I forgot to include my gist. There are more errors after the
f
error
a
I think this worked at some point, but as of now you either define a new version like @Kristian Frøhlich did, or you nest several
with
we should definitely check the documentation, though