```context(A, B, C, D, E, F, G, H, I, J) someFunct...
# language-proposals
j
Copy code
context(A, B, C, D, E, F, G, H, I, J)
someFunction() { ... }
Copy code
with (a) {
    with (b) {
        with (c) {
            with (d) {
                with (e) {
                    ...
                    with(j) {
                        someFunction()
                    }
                }
            }
        }
    }
}
I was thinking
with (a & b & c & d & e & g & h & i & j)
or
contexts (a, b, c, d, e, g, h, i, j)
that automatically generates that nested withs, but that's even messier obviously I've exaggerated the number of params, but if context receivers were to be used as a replacement for dependency injection (which seems like a brilliant use-case), I can easily see the number of params increasing as the number of services in a project increases. For now, the obvious workaround is just stashing all those fields in a single class and doing something like this:
Copy code
context(Alphabet)
fun someFunction() {
    this@Alphabet.a.doSomethingWithA();
}
Copy code
with(Alphabet(a = TODO(), b = TODO(), ..., z = TODO())) {
    someFunction()
}
Maybe this can become
Copy code
context class Alphabet(
    val a: A
    val b: B
    ...
    val z: Z
)
now if you
context(Alphabet)
, it puts all of those fields in context instead of the Alphabet class itself like a data class would have done. Also, if something has
context(A)
, I can just pass the whole
Alphabet
context instead of wrapping the call-site in
with (this@Alphabet.a) { ... }
since Alphabet already contains
A
b
This comment from @elizarov about scoping functions (e.g. with) and context receivers makes me think there could be a solution in the works
❤️ 4
y
You already can define a multi-with. I've been using it for months and it's been great!
Copy code
inline fun <A, B, C, D, R> withContexts(a: A, b: B, c: C, d: D, block: context(A, B, C, D) () -> R): R {
  contract {
    callsInPlace(block, InvocationKind.EXACTLY_ONCE)
  }
  return block(a, b, c, d)
}
(You might need to suppress a subtyping-between-contexts error, but otherwise it works really well)
❤️ 4
e
sweet, that's really nice, @Youssef Shoaib [MOD] thanks for the tips