Ive Vasiljevic
12/06/2019, 8:54 AMclass User(private val map: MutableMap<String, String>) {
val name: String by map
val lastName: String by map
}
class User1(private val map: MutableMap<String, String>) {
val name: String
val lastName: String
init {
name = map["name"].toString()
lastName = map["lastName"].toString()
}
}
So basically, when accessing property "name" from class User in the fun main(). It is using MutableMaps default getValue() implementation to get the value?fun main() {
val user = User(mutableMapOf("name" to "Ive",
"lastName" to "Vasilj"))
val user1 = User1(mutableMapOf("name" to "Ive",
"lastName" to "Vasilj"))
println(user.name)
println(user.lastName)
println(user1.name)
println(user1.lastName)
}
Dominaezzz
12/06/2019, 10:41 AMgetValue
.map
is mutated, the first example will reflect the changes but the second won't. It will only have the initial values.class User(private val map: MutableMap<String, String>) {
val name: String get() = map["name"]
val lastName: String get() = map["lastName"]
}
karelpeeters
12/06/2019, 11:43 AMtoString()
to fix nullability, it actually calls getValue()
.