I am using JGraphT library from Kotlin and I want to have my graph parametrised. However, the way I was trying to do it does not work, since U is not defined at the compile time and can not be used for reflection. So I get the error message "Cannot use T as a reified type parameter. Use a class instead." As far as I know, reified type parameters can be used for inline functions to resolve this, but I can not see how it could help me here, especially knowing that I can not change the library code. Any ideas would be appreciated.
class GraphManipulation<T,U> {
val g = DefaultDirectedGraph<T, U>(U::class.java)
...}
The problem is not with the use of DefaultDirectedGraph
, but that U
is not reified in your GraphManipulation
class. Since classes can't have reified class parameters (yet?) you need to take the class as a constructor parameter:
class GraphManipulation<T,U>(private val uClass: Class<U>) {
val g = DefaultDirectedGraph<T, U>(uClass)
}
Where reified
can help is to make a helper method
inline fun <T, reified U> GraphManipulation(): GraphManipulation<T,U> = GraphManipulation(U::class.java)