05nelsonm / encoding

A Kotlin Multiplatform library for configurable, streamable, efficient and extensible Encoding/Decoding with support for base16/32/64.
Apache License 2.0
33 stars 5 forks source link

Use generics with `EncoderDecoder` to pass `Config` type #74

Closed 05nelsonm closed 1 year ago

05nelsonm commented 1 year ago

Accessing the EncoderDecoder.Config always requires a cast because there is no type specified for EncoderDecoder. This also occurs within a Feed now that #70 made EncoderDecoder.Feed.config public.

public sealed class Decoder<C: EncoderDecoder.Config>(public val config: C) {

    public abstract fun newDecoderFeed(out: OutFeed): Decoder<C>.Feed

    public inner class Feed: EncoderDecoder.Feed<C>(config) {
        // ...
    }

    // ...
}

public sealed class Encoder<C: EncoderDecoder.Config>(config: C): Decoder(config) {

    public abstract fun newEncoderFeed(out: OutFeed): Encoder<C>.Feed

    public inner class Feed: EncoderDecoder.Feed<C>(config) {
        // ...
    }

    // ...
}

public abstract class EncoderDecoder<C: Config>(config: C): Encoder(config) {
    public abstract class Config(
        // ...
    ) {
        // ...
    }

    public sealed class Feed<C: Config>(public val config: C) {
        // ...
    }
}

Need to think on this more because it affects user experience with the library. NOT doing it and requiring a cast all the time also affects user experience... so could go both ways.