I'm lost in generic types many hours. I want to simplified the code but I can't cast object when its generic type. How I can solve this problem or another simplified way? (I'm use kotlin 1.2)
sealed class SETTING(override val key: String) : SettingsKeyContract
object optFirstLaunch : SETTING("first_launch"), SettingsKeyContractWithType<Boolean> {
override val defaults = true
}
object optNotifications : SETTING("notification_list"), SettingsKeyContractWithType<List<String>> {
override val defaults = emptyList<String>()
}
interface SettingsKeyContract { val key: String }
interface SettingsKeyContractWithType<T : Any> : SettingsKeyContract {
val defaults: T
@Suppress("UNCHECKED_CAST")
fun get(): T? = (App.getContentComponent()?.getSettings()?.get(key)?.value?.data as? T)
fun remove() = (App.getContentComponent()?.getSettings())?.delete(key)
fun save(value: T) = (App.getContentComponent()?.getSettings()?.add(key, value))
}
class OptionModel(@OPTIONS_ID optionId: Int, contract: SettingsKeyContract)
val optionModel = OptionModel(1, optNotifications)
when(optionModel.contract){
is SettingsKeyContractWithType<List<String>> -> (optionModel.contract as SettingsKeyContractWithType<List<String>>).set(listOf("ring-1", "ring-2")) //error
is SettingsKeyContractWithType<Boolean> -> (optionModel.contract as SettingsKeyContractWithType<Boolean>).set(true) //error
}
Error:
Cannot check for instance of erased type: SettingsKeyContractWithType<...>