scoping, my dude
# announcements
s
scoping, my dude
m
Extension function scoping is really powerful
s
you could transform this into an example of context oriented programming:
Copy code
fun main() {
    val x = "test"
    with(Test) {
        val y: String = x[2..3]
    }
}

object Test {
    operator fun String.get(range: IntRange) = substring(range)
}
👍 1
m
elaborating on @Stephan Schroeder's answer, if you want to reference
y
outside the
with
block, you can do
Copy code
fun main() {
    val x = "test"
    val y: String = with(Test) {
        x[2..3]
    }
    println(y)
}

object Test {
    operator fun String.get(range: IntRange) = substring(range)
}
👍 1