By using delegating properties, first example is t...
# getting-started
i
By using delegating properties, first example is the same as the second example without using delegates.
Copy code
class 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?
Copy code
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)
  
}
d
Yes it is calling
getValue
.
The first example and second example are not quite the same. If
map
is mutated, the first example will reflect the changes but the second won't. It will only have the initial values.
☝️ 3
The second example should be more along the lines of this.
Copy code
class User(private val map: MutableMap<String, String>) {
    val name: String get() = map["name"]
    val lastName: String get() = map["lastName"]
}
k
Also it doesn't just call
toString()
to fix nullability, it actually calls
getValue()
.