data class Message (var code: String, var msg: String, var data: Any) {
fun addDataItem(dataItem: Any): Message {
if (this.data is Collection<*>) {
this.data = this.data as Collection<*> + dataItem // ok, but as-operation is essential
return this
}
return this.copy(data = listOf(dataItem))
}
fun addDataEntry(name: String, value: Any): Message {
if (this.data is Map<*, *>) { // can reified to Map<String,Any> ?
this.data = this.data as Map<*, *> + (name to value) // compiler error here
}
return this.copy(data = mapOf(name to value))
}
}
the map operation in the fun addDataEntry() is simple, but I can’t figure out the right way to achieve.
Thanks, forward