What is Kotlin Reified Type Parameter?

Simple example to understand reified type parameters in Kotlin

One of the most common uses of this reified type parameter is view model creation. For example, the following code is the viewModels() composable helper function to create the view model.


This content originally appeared on DEV Community and was authored by Vincent Tsen

Simple example to understand reified type parameters in Kotlin

One of the most common uses of this reified type parameter is view model creation. For example, the following code is the viewModels() composable helper function to create the view model.

@Composable
public inline fun <reified VM : ViewModel> viewModel(
    viewModelStoreOwner: ViewModelStoreOwner 
        = checkNotNull(LocalViewModelStoreOwner.current) {
        "No ViewModelStoreOwner was provided via LocalViewModelStoreOwner"
    },
    key: String? = null,
    factory: ViewModelProvider.Factory? = null
): VM = viewModel(VM::class.java, viewModelStoreOwner, key, factory)

It has 3 parameters, and it calls another viewModel(), but let's simplify this view model creation for the sake of understanding this reified.

Assuming you have

class MyViewModel {}

Class Type Parameter

and you want to create an instance of MyViewModel by its class type (i.e. MyViewModel::Class.java), you want to call (MyViewModel::class.java).getDeclaredConstructor().newInstance().

To make it a helper function, you create the following:

fun <T>viewModel(classType: Class<T>): T {
    return classType.getDeclaredConstructor().newInstance()
}

To create the view model, you call viewModel(MyViewModel::class.java)

val viewModel = viewModel(MyViewModel::class.java)

You cannot use T::class.java directly, thus you need to pass in as Class<T> parameter into the viewModel() function.

This limitation is called Type erasure because the JVM platform doesn't support generic types. The generic type is lost when we compile Kotlin to JVM bytecode.

However, Kotlin overcomes this limitation with Reified type parameter.

Reified Type Parameter

Instead of using the class type parameter above, you can use the reified type parameter. So you change the viewModel() helper function to the following.

inline fun <reified T> viewModel(): T {
    return (T::class.java).getDeclaredConstructor().newInstance()
}

Please note that you must specify the inline.

With reified T, you can access the class type (i.e.T::class.java) and you don't need to pass it in as in the previous example.

To create a view model, you call viewModel<MyViewModel>()

val viewModel = viewModel<MyViewModel>()

or you can also call viewModel() direction without passing the class name, but you need to declare the viewModel variable type which allows the Kotlin compiler to infer it.

val viewModel: MyViewModel = viewModel()

Conclusion

Inline reified type parameter is another fancy way of passing class type(i.e. Class<T>) in your function. The caller no longer needs to explicitly pass in the class type parameter anymore into a function.

Originally published at https://vtsen.hashnode.dev.


This content originally appeared on DEV Community and was authored by Vincent Tsen


Print Share Comment Cite Upload Translate Updates
APA

Vincent Tsen | Sciencx (2023-03-24T23:00:00+00:00) What is Kotlin Reified Type Parameter?. Retrieved from https://www.scien.cx/2023/03/24/what-is-kotlin-reified-type-parameter/

MLA
" » What is Kotlin Reified Type Parameter?." Vincent Tsen | Sciencx - Friday March 24, 2023, https://www.scien.cx/2023/03/24/what-is-kotlin-reified-type-parameter/
HARVARD
Vincent Tsen | Sciencx Friday March 24, 2023 » What is Kotlin Reified Type Parameter?., viewed ,<https://www.scien.cx/2023/03/24/what-is-kotlin-reified-type-parameter/>
VANCOUVER
Vincent Tsen | Sciencx - » What is Kotlin Reified Type Parameter?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/03/24/what-is-kotlin-reified-type-parameter/
CHICAGO
" » What is Kotlin Reified Type Parameter?." Vincent Tsen | Sciencx - Accessed . https://www.scien.cx/2023/03/24/what-is-kotlin-reified-type-parameter/
IEEE
" » What is Kotlin Reified Type Parameter?." Vincent Tsen | Sciencx [Online]. Available: https://www.scien.cx/2023/03/24/what-is-kotlin-reified-type-parameter/. [Accessed: ]
rf:citation
» What is Kotlin Reified Type Parameter? | Vincent Tsen | Sciencx | https://www.scien.cx/2023/03/24/what-is-kotlin-reified-type-parameter/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.