What are the benefits of the following model, when non-generic sublasses have parent generic superclass/interface?
interface Generic<Q: Number, R: Number> {
fun calculate(input: Q): R
}
class ClassA: Generic<Int, Int> {
override fun calculate(input: Int): Int {
return input
}
}
class ClassB: Generic<Float, Float> {
override fun calculate(input: Float): Float {
return input
}
}
Polymorphism is not available anyway (need explicit casting with pattern matching). Same result is possible to get without inheritance/implementation:
fun main() {
val classA: Generic<Int, Int> = ClassA()
val classB: Generic<Float, Float> = ClassB()
val inputs: List<Number> = listOf(1, 1.1, 2, 2.2)
for (input in inputs) {
when (input) {
is Int -> classA.calculate(input)
is Float -> classB.calculate(input)
}
}
}