I am very new to Kotlin. I have a problem accessing fields of a variable passed as a parameter in a generic method of a class derived from an abstract base class.
Given:
(1) a class, whose fields I can’t access:
package com.pds.service.model
import kotlinx.serialization.Serializable
@Serializable
data class BaseDefaults(val pdsType: String, val setName: String,
val coverages: MutableMap<String, String>,
var policyDeductible: String,
val discounts: MutableMap<String, String>)
(2) A base abstract class containing generic method:
package com.pds.service.model
import kotlinx.serialization.Serializable
@Serializable
abstract class DeltaOperation<S>() {
abstract fun <S> applyOperation(defaults: S) : S
fun updatePropertiesMap(baseProperties: MutableMap<String, String>,
deltaProperties: Map<String, String>) {
for ((targetName, targetValue) in deltaProperties) {
val coverage = baseProperties[targetName]
if (coverage == null) {
baseProperties[targetName] = targetValue
} else {
if (targetValue.isEmpty()) {
baseProperties.remove(targetName)
}
else {
baseProperties.replace(targetName, targetValue)
}
}
}
}
}
(3) I get “Unresolved reference” errors trying to access defaults.coverages, defaults.policyDeductible and defaults.discounts of BaseDefaults in the implementation of the abstract method in the following class:
package com.pds.service.model
import kotlinx.serialization.Serializable
@Serializable
class BaseDefaultsDeltaOperation(
val coverages: Map<String, String>? = null,
val policyDeductible: String? = null,
val discounts: Map<String, String>? = null
) : DeltaOperation<BaseDefaults>() {
override fun <BaseDefaults> applyOperation(defaults: BaseDefaults) : BaseDefaults {
// process coverages
if (coverages != null) updatePropertiesMap(defaults.coverages, coverages)
// process policy deductible
if (policyDeductible != null) defaults.policyDeductible = policyDeductible
// process discounts
if (discounts != null) updatePropertiesMap(defaults.discounts, discounts)
return defaults
}
}
What am I doing wrong? Thank you.