Tonis Ives
09/05/2023, 3:20 AMEffects and contexts
part of the documentation. Docs seem to indicate that with
function can be used by multiple arguments
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
public inline fun <T, R> with(receiver: T, block: T.() -> R): R {
Which with
function should I use?simon.vergauwen
09/05/2023, 6:55 AMwith
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 MenaTonis Ives
09/05/2023, 8:36 AMKristian Frøhlich
09/05/2023, 9:15 AM@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>
}
simon.vergauwen
09/05/2023, 9:16 AMsimon.vergauwen
09/05/2023, 9:16 AMKristian Frøhlich
09/05/2023, 9:18 AMTonis Ives
09/05/2023, 11:50 PMf
errorAlejandro Serrano.Mena
09/06/2023, 7:59 AMwith
Alejandro Serrano.Mena
09/06/2023, 7:59 AM