Hello everyone.
There are situations where it is useful having a class with a generic type parameter that holds some kind of information or has some kind of behaviour related to the generic parameter, like being able to produce the class name or create a new instance of the generic type.
Because, currently, kotlin does not support reified type parameters for classes, a common solution for this problem is to pass a class object with the same type as the generic type to the constructor. To illustrate this idea, consider the following class. Bear in mind that this is a toy example and it is simple so that it is easy to understand.
open class A<T>(private val type: Class<T>) {
val name: String
get() = type.name
fun newInstance(): T = type.newInstance()
}
If we need to subclass the previous class we would do something like the following:
class StringA : A<String>(String::class.java) {
fun aMethod() {
// code
}
}
Notice that we have to reference the String class twice: once as type parameter and again to get its corresponding java Class instance, even though, we know that the only Class instance that is reasonable to be passed to the constructor has to be the String Class instance.
It would be nice if we instead simply subclass A like this:
class StringA : A<String>() {
fun aMethod() {
// code
}
}
To allow this, i suggest the implementation of inline constructors in kotlin that would allow reified type parameters to be passed to the constructor.
Going back to class A, if it had inline constructors, it could be something like this:
open class A<T>(private val type: Class<T>) {
inline constructor<reified T>() : this(T::class.java)
val name: String
get() = type.name
fun newInstance(): T = type.newInstance()
}
class StringA : A<String>() // subclassing using inline constructor
It is important to notice, that for this to work, the inline constructor must reference a “concrete” constructor for which it will be inlined.
Also, “reified T” in the constructor should be a type parameter of the constructor, however, and considering that, currently, kotlin does not support type parameters on constructors, this could reference the class generic type parameter.
What do people think about this?