Let us say we have a class Data
that holds either nullable, or not-null values with generics.
class Data<T>(val data: T)
fun <T> Data<T>.get(): T = data
fun main() {
val example = Data<String?>("Hello")
example.get() // Nullable - returns String?
val example2 = Data("Hello") // Obviously implicitly the same as Data<String>("Hello")
example2.get() // NotNull - returns String
}
Now this works and is almost exactly what I want (especially Syntax). But I need the type of the KClass too. Which gives us
class Data<T>(val data: T, val kclass: KClass<T>)
That won’t work since T
of KClass
is not within T: Any
.
- I want the nullability determined by the constructor
- I want the generic type of
KClass
- Optimally only a single Type Parameter for both
KClass<T>
andval data: T
So the optimal solution would be to get the non-null type of T with “T!” or some other imaginary thing, but I doubt that is possible.
class Data<T>(val data: T, val kclass: KClass<T!>)
Using multiple generics, one for KClass<T>
, the other for val data: T
would not be elegant, and I hope to get around that in some way. I do not want to type the generic twice to instantiate a Data object.
I played around with reified
, but I just could not come up with something elegant. I know you can use <reified T>
in an inline function and get the class of T even if T is <T: Any?>
(T::class
). Maybe you could make it work with that in some way?
Does anyone know an elegant solution to this?
Thank you in advance!