https://kotlinlang.org logo
#announcements
Title
# announcements
s

Shawn

06/12/2019, 2:00 PM
scoping, my dude
m

Marko Mitic

06/12/2019, 2:10 PM
Extension function scoping is really powerful
s

Stephan Schroeder

06/12/2019, 2:13 PM
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

max

06/12/2019, 3:47 PM
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
5 Views