Open lnhrdt opened 3 years ago
subclass(Item::class)
does not allow null values — it's a serializer for Response<Item>
, not Response<Item?>
. Try subclass(Item.serializer().nullable)
Changing my example to:
serializersModule = SerializersModule {
polymorphic(Any::class) {
subclass(Item.serializer().nullable)
}
}
Results in this compilation error:
None of the following functions can be called with the arguments supplied. PolymorphicModuleBuilder<TypeVariable(Base)>.subclass(KClass<TypeVariable(T)>) where Base = TypeVariable(Base), T = TypeVariable(T) for inline fun <Base : Any, reified T : Base> PolymorphicModuleBuilder
.subclass(clazz: KClass ): Unit defined in kotlinx.serialization.modules PolymorphicModuleBuilder<TypeVariable(Base)>.subclass(KSerializer<TypeVariable(T)>) where Base = TypeVariable(Base), T = TypeVariable(T) for inline fun <Base : Any, reified T : Base> PolymorphicModuleBuilder .subclass(serializer: KSerializer ): Unit defined in kotlinx.serialization.modules
Thanks for trying to help. Any more ideas?
Hm, looks like that this part wasn't well thought out. You can try subclass<Item>(Item.serializer().nullable as KSerializer<Item>)
but now I'm not sure that we support nullable polymorphism at all
@sandwwraith I tried your suggestion but it fails with the same error
null cannot be cast to non-null type kotlin.Any
Thanks for the fast help, even if my scenario turns out to not be supported in this library. Is it likely to be supported soon?
To understand how to use polymorphism and generic classes I've followed the section on it in the polymorphism docs. However when the generic value is null it throws an exception. Here's an example based on the code in the docs.
exception: