많은 시간에 제네릭 형식이 손실됩니다. 나는 코드를 단순화하고 싶지만 객체를 일반 타입으로 변환 할 수 없다. 이 문제 또는 다른 단순화 된 방법을 어떻게 해결할 수 있습니까? (I'm use kotlin 1.2)
Kotlin 제네릭 형식 문제
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
}
오류 :
Cannot check for instance of erased type: SettingsKeyContractWithType<...>