Reputation: 1902
I send such list to the function: listOf(IncomeVerificationType::class, IncomeType::class)
, but function returns the map only with "id"
. Why is is
not working? What am I doing wrong?
Function:
fun sendUpdatedEvent(model: Model, updatedFields: List<Any>): Map<String, Any> {
val fieldToValueMap = mapOf("id" to model.identity.id)
updatedFields.forEach {
when (it) {
is IncomeVerificationType -> fieldToValueMap.plus("incomeVerification" to model.incomeVerification)
is IncomeType -> fieldToValueMap.plus("incomeTypeId" to model.incomeTypeId)
}
}
return fieldToValueMap
}
Upvotes: 0
Views: 79
Reputation: 6495
Because IncomeType::class
is not an instance of IncomeType
.
The type of IncomeType::class
is KClass<IncomeType>
. It references the class itself, not an instance of the class.
To correctly match those class objects in a when statement you can use the regular equality checks instead of the 'is' check:
val list = listOf(IncomeVerificationType::class, IncomeType::class)
list.forEach {
when (it) {
IncomeVerificationType::class -> println("IncomeVerificationType")
IncomeType::class -> println("IncomeType")
}
}
Upvotes: 2