Storing Properties in a Map

 

In Kotlin we might want to store our properties in a map. Let's take a look into example that does that (MapProperties.kt):

 

class Employee(val map: Map<String, Any?>) {

val firstName: String by map

val lastName: String by map

val yearOfBirth: Int by map

}

 

fun tryMapProperties() {

val e = Employee(

mapOf(

"firstName" to "John",

"lastName" to "Smith",

"yearOfBirth" to 1985

)

)

 

println("Employee [ ${e.firstName} ${e.lastName} ][ ${e.yearOfBirth} ]")

}

 

Console output:

 

Employee [ John Smith ][ 1985 ]