What if your functions could magically remember the exact type you want without extra work?
Why Reified type parameters with inline in Kotlin? - Purpose & Use Cases
Imagine you want to write a function that works with different types, but you need to know the exact type at runtime. Without special tools, you have to pass the type information manually every time, like carrying a map in your pocket to find your way.
This manual way is slow and error-prone. You might forget to pass the type or pass the wrong one. It feels like repeating the same instructions over and over, making your code bulky and hard to read.
With reified type parameters and inline functions, Kotlin lets you keep the type information inside the function automatically. It's like having a smart assistant who remembers the type for you, so you don't have to carry it around or repeat yourself.
fun <T> parse(value: String, clazz: Class<T>): T { return clazz.cast(value) }
parse("123", Int::class.java)inline fun <reified T> parse(value: String): T { return value as T }
parse<Int>("123")This makes your code cleaner and safer by letting you use type information directly inside generic functions without extra hassle.
For example, when converting JSON strings to objects, you can write one function that automatically knows which object type to create, without passing the type explicitly every time.
Manual type passing is repetitive and error-prone.
Reified type parameters keep type info at runtime inside inline functions.
This leads to simpler, safer, and cleaner generic code.