Given this code:
// Can generic type parameter be inferred?
class Foo<T> {
fun consume(p: T) {
println("slurp $p")
}
}
//region
abstract class ATestsOfFoo<U: Foo<T>, T>(private val foo: U) {
fun test1() {
foo.consume(someT())
}
abstract fun someT(): T
}
class TestsOfFoo(foo: Foo<String>): ATestsOfFoo<Foo<String>, String>(foo) {
override fun someT(): String {
return "Hello"
}
}
val testsOfFoo = TestsOfFoo(Foo())
testsOfFoo.test1()
//endregion
I'm looking for a way to infer T type parameter from U.
Said differently, I'd like to be able to write the following line without repeating String as the type parameter:
...
// This is an example of what I'd like to be able to write, it does not work
class TestsOfFoo(foo: Foo<String>): ATestsOfFoo<Foo<String>>(foo) {
...
Is there a way to do so?
If not, is there a conceptual flaw that would forbid us to safely infer T from U?
P.S:
I know we could simplify ATestsOfFoo like this:
abstract class ATestsOfFoo<T>(private val foo: Foo<T>) {
...
however I'm not looking for that, conceptually ATestsOfFoo operates on Foo<T>, I'd like to keep that information in type parameters.