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
Fergus Hewson
10/15/2024, 7:14 PM
Thanks for the clarification
j
Jokubas Alekna
10/25/2024, 12:05 PM
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
CLOVIS
10/25/2024, 12:20 PM
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