Multiple Scope "With" function

fun <A, B, C, R> with(
    s1: A,
    s2: B,
    s3: C,
    block: @ContextFunctionTypeParams(count = 3) () -> R
): R =
    with(s1) {
        with(s2) {
            with(s3) {
                block.invoke()
            }
        }
    }


fun somefun() {
    val lambda: context(String, Int, Boolean) () -> Unit = {
        TODO()
    }

    with(
        "1",
        2,
        false,
        lambda
    )
}

to make easy with context receiver. i tried to make this but didn’t work.

@Suppress("SUBTYPING_BETWEEN_CONTEXT_RECEIVERS")
public inline fun <A, B, C, R> with(a: A, b: B, c: C, block: context(A, B, C) () -> R): R {
  contract {
    callsInPlace(block, InvocationKind.EXACTLY_ONCE)
  }
  return block(a, b, c)
}
1 Like