kotlinx.serialization icon indicating copy to clipboard operation
kotlinx.serialization copied to clipboard

Make DeserializationStrategy covariant at declaration-site

Open lukellmann opened this issue 3 years ago • 0 comments

The type parameter T of DeserializationStrategy only appears in an out position, namely as the return type of deserialize(). It can therefore be annotated with out to increase API flexibility compared to DeserializationStrategy being invariant. E.g. a function taking DeserializationStrategy<Number> would now also accept an object of type DeserializationStrategy<Int>.

This would also result in DeserializationStrategy being more similar to the symmetric SerializationStrategy interface which is already contravariant at declaration-site (interface SerializationStrategy<in T>), SerializationStrategy is a consumer, DeserializationStrategy a producer.

This change is

  • [x] binary backwards compatible: generics are erased at runtime
  • [x] source backwards compatible: previously specified out projections (use-site covariance) will only report a warning about a redundant projection

Please tell me when there is a reason why this shouldn't be changed or wasn't changed before, maybe I'm missing something.

lukellmann avatar Apr 07 '22 01:04 lukellmann