August 2018
Beginner
178 pages
4h 56m
English
In the previous section, we described how you cannot know what generic type arguments are used during runtime because of type erasure. The following code tries to get the class type of the generic argument, but will not compile because the runtime doesn’t have information about the generic T type that will be used when invoking this function:
fun <T> createInstance(): T { //compiler error return T::class.java.newInstance()}
Knowing generic type information can sometimes be useful and Kotlin can bypass this limitation with reified generics. A generic type can be reified with inline functions. We explained how inlining works in the previous chapter.
Read now
Unlock full access