Default Types for Generics


#1

I could not find an existing topic regarding this issue but maybe my keywords were simply not the right ones to search for. Regardless, here it comes.

I would love to see default types for generics in Kotlin like Rust has it:

The most prominent example that could make use of this would be Comparable<T> by changing it to Comparable<T = this> but there are more interesting use cases.

sealead class S
class A : S()
class B : S()

open class X<T : A>
class Y : X<B>()

Constructing X is not possible without specifying the concrete type right now, hence, one has to write X<A>() everywhere. This is cumbersome and repetitive and something that was clear for X from the very beginning.

open class X<T : A = A>
class Y() : A<B>()

Now it is possible to construct X without specifying A everywhere: X()


#2

#3

I didn’t expect that that topic would contain the same problem judging from the title.