Local functions are incredibly useful for separating code in a function that does not need to be exposed elsewhere:
fun parse(str: String) {
val someList = ArrayList<String>() // variable seen inside local functions as well
fun parsePrefix() {...}
fun parseSuffix() {...}
for (word in str.split('|')) {
parsePrefix()
// do more stuff
parseSuffix()
}
}
This helps structuring code inside a complex function, making it more readable and concise. And sometimes, there’s also a variable you’d like to access in multiple places, with some custom set/get code. Currently, this is the only way to do it:
fun parse(str: String) {
var padding = 0 // gets changed
val someList = ArrayList<String>()
fun setPadding(pad: Int) {
padding = pad.coerceIn(0 until someList.length)
}
fun first() = someList[padding]
val someValue = padding
// must not forget to set padding with setter
setPadding(someOtherValue)
first().doSomething()
}
Considering we have local functions, why would we not have local properties as well?
fun parse(str: String) {
val someList = ArrayList<String>()
var padding = 0 set(value) { field = value.coerceIn(0 until someList.length) }
val first get() = someList[0]
val someValue = padding
// setting is easier now
padding = someOtherValue
first.doSomething()
}
Under the hood, these local properties could just be compiled in the same way local functions are (just like how normal properties get compiled to a field, setter and getter).