I'm building an ORM for use with jasync-sql in Kotlin and there's a fundamental problem that I can't solve. I think it boils down to:
How can one instantiate an instance of a class of type
T
, given a non-reified type parameterT
?
The well known Spring Data project manages this and you can see it in their CrudRepository<T, ID>
interface that is parameterised with a type parameter T
and exposes methods that return instances of type T
. I've had a look through the source without much success but somewhere it must be able to instantiate a class of type T
at runtime, despite the fact that T
is being erased.
When I look at my own AbstractRepository<T>
abstract class, I can't work out how to get a reference to the constructor of T
as it requires accessing T::class.constructors
which understandably fails unless T
is a reified type. Given that one can only used reified types in the parameters of inline functions, I'm a bit lost as to how this can work?