Marc Knaup
10/23/2017, 9:47 AMclass SomeDecoder<out SomeContext = Nothing>(
val context: SomeContext
) {
inline fun <reified T = Any?> decodeValue(): T {
// …
}
}
fun main() {
val decoder: SomeDecoder // aka SomeDecoder<Nothing>
if (…)
decoder = …
else
decoder = …
// …
decodeWith(decoder)
}
fun decodeWith(decoder: SomeDecoder) { // aka : SomeDecoder<Nothing>
val anyValue = decoder.decodeValue() // type inference succeeds because Any? is default
val boolValue: Boolean = decoder.decodeValue() // type inference overrides default
// …
}
My JSON library has several interfaces which take a Context
parameter but it’s just unnecessary work and clutter for the user having to write <ContextInterface>
(or however the base interface is called - just Any?
in the example) on every type they use if they never use/care about the context and just want to use the simple parts of the API which is context-agnostic.
I could split each interface up into two - one with and one without context, but’s quite a lot of work for such a simple task.