https://kotlinlang.org logo
#getting-started
Title
# getting-started
l

LastExceed

10/27/2021, 12:55 PM
why are local extension properties not allowed, but functions are ?
Copy code
fun foo() {
	fun Int.x() = 42 //ok
	val Int.y = 42 //error
}
j

Joffrey

10/27/2021, 12:57 PM
I don't know, but my guess would be that the
get()
syntax might be a problem in function bodies (they would not mix well with regular code). And extension properties can't be defined with backing fields like you did even in general, they always need a getter
l

LastExceed

10/27/2021, 12:58 PM
makes sense
e

ephemient

10/28/2021, 5:15 AM
extension properties must always be getter (and setter) only, because they cannot modify the receiving class to add a backing field
I don't know why local extension properties are not allowed at all, they just desugar to function calls… oh well. I see a request from years ago: https://youtrack.jetbrains.com/issue/KT-23711
259 Views