class GetMe {
var a = 1
operator fun getValue(thisRef: Any?, property: KProperty<*>): Int {
return ++a
}
}
private val getMe = GetMe()
private val testMe: Int by getMe
@Test
fun `I'm a playground, not a test`() {
println(testMe)
println(testMe)
println(testMe)
}
We can change testMe values although it is declared as val. Shouldn’t Kotlin compiler take care of this case and not allow this kind of delegate?
This looks like a workaround to me but val is immutable given
val a = 10
a = 20
throws an error in the snippet given we are age does not practically exist as it is fetched from an already declared value i.e _age which is the one being fetched.
Yes, in this example val a is an immutable value. However that does not mean that val itself declares makes a value immutable. As pointed out by Jonathan Haas and arocnies val makes a value readonly. In your example there is no way of changing this readonly value, therefor it’s immutable.
Also there is a small difference between properties and values inside of functions. You could very well argue that a simple value inside of a function is immutable (as long as you don’t declare it using a delegate or custom getter), but that’s not really what the OP was asking about.
Kotlin’s abstraction in general only allows for the distinction between readonly and mutable. Kotlin itself does not have a concept of immutable. This is also the case for List vs MutableList, where many people confuse the first for being immutable, which is wrong.