as it's possible to have multiple context elements...
# language-evolution
g
as it's possible to have multiple context elements
context(A, B)
, it would be convenient to have mutliple elements in with:
with(a, b) { ... }
f
There is only one this. You could merge them together if a/b are both abstract/interfaces.
c
Perhaps you are looking for the upcoming context parameters feature?
👌 2
🚫 1
c
@Fergus Hewson you can have any number of
this
, even right now:
Copy code
class Foo {

    fun showFoo() {
        println(this)
    }

    inner class Bar {

        fun showBar() {
            println(this)
        }

        fun exec() {
            with(Baz()) {
                showFoo()
                showBar()
                showBaz()
            }
        }
    }

    class Baz {

        fun showBaz() {
            println(this)
        }

    }
}

Foo().Bar().exec()
✅ 1
f
Thanks for the clarification
j
This would require vararg type parameters to be added. I think the Kotlin team mentioned that they had a prototype for this feature, so it might be coming in a future language version
c
There are many ways to do this, with various amount of language support. One option in the KEEP was to create a new keyword to inject a value into scope:
Copy code
fun foo {
    with LoggerScope()
    <http://log.info|log.info> { "Test" }
}
Compared to the
with(a, b)
approach, this has the benefits that: • No need to invent a new concept of n-ary type parameters (which are a very complex can of worms) • This is purely syntax sugar, so there is no need for binary compatibility or anything else • Doesn't force us to introduce a new indentation level each time we want to add something in the context