← Back to Kotlin Course | Chapter 11: Generics | Lesson 4 of 6

Reified Type Parameters क्या हैं

Reified type parameters किसी inline function को असल में runtime पर यह जांचने देते हैं कि उसे किस type से call किया गया था, ऐसा कुछ जो normal generics नहीं कर सकते।
Syntax
kotlin
inline fun <reified T> functionName(value: Any): Boolean {
    return value is T            // T is available at runtime
}

inline fun <reified T> typeName(): String? = T::class.simpleName

The Type Erasure Problem

JVM पर, generic type information आमतौर पर runtime पर erase हो जाती है, इसलिए एक regular generic function if (value is T) नहीं जांच सकता -- compiler को compile होने के बाद यह पता नहीं होता कि T असल में क्या है।

उदाहरण: The Type Erasure Problem

markup
fun <T> describe(value: Any): String {
    // Return `"Checking value: $value"` from this function
    return "Checking value: $value"
}

// Entry point: execution of the program starts here
fun main() {
    // Print `describe<String>("Kotlin")` to the console, with a trailing newline
    println(describe<String>("Kotlin"))
}

Reified Solves It with inline

किसी inline function पर type parameter को reified चिह्नित करना हर call site पर असली type को संरक्षित करता है, क्योंकि function का code (और असली type) सीधे caller में copy हो जाते हैं।

उदाहरण: Reified Solves It with inline

markup
inline fun <reified T> isInstanceOf(value: Any): Boolean = value is T

// Entry point: execution of the program starts here
fun main() {
    // Print `isInstanceOf<String>("Kotlin")` to the console, with a trailing newline
    println(isInstanceOf<String>("Kotlin"))
    // Print `isInstanceOf<Int>("Kotlin")` to the console, with a trailing newline
    println(isInstanceOf<Int>("Kotlin"))
}

Using T::class with Reified

एक reified type parameter सीधे T::class access करने की भी अनुमति देता है, जो logging, reflection-lite checks, या generic factories बनाने के लिए उपयोगी है।

उदाहरण: Using T::class with Reified

markup
inline fun <reified T> typeName(): String = T::class.simpleName ?: "Unknown"

// Entry point: execution of the program starts here
fun main() {
    // Print `typeName<String>()` to the console, with a trailing newline
    println(typeName<String>())
    // Print `typeName<Int>()` to the console, with a trailing newline
    println(typeName<Int>())
}

Filtering a List by Reified Type

Reified generics का एक आम practical उपयोग किसी मिश्रित collection को सिर्फ एक specific runtime-checked type के elements तक filter करना है।

उदाहरण: Filtering a List by Reified Type

markup
inline fun <reified T> filterByType(items: List<Any>): List<T> = items.filterIsInstance<T>()

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `mixed`, set to `listOf(1, "two", 3, "four", 5)`
    val mixed = listOf(1, "two", 3, "four", 5)
    // Declare a read-only variable `strings`, set to `filterByType<String>(mixed)`
    val strings = filterByType<String>(mixed)
    // Print `strings` to the console, with a trailing newline
    println(strings)
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. किसी regular (non-inline) generic function में T::class या is T उपयोग करने की कोशिश करना, जो compile होने में fail होता है क्योंकि type runtime पर erase हो जाता है।
  2. यह भूल जाना कि reified सिर्फ किसी inline function के type parameter पर उपयोग किया जा सकता है, क्योंकि type information inlining से संरक्षित होती है, खुद JVM से नहीं।
  3. reified generics का ज़्यादा उपयोग करना जहां explicitly पास किया एक साधारण Class<T> parameter भी उतना ही अच्छा काम करता और function को non-inline रखता।
चैप्टर सारांश
  • किसी inline function पर type parameter को reified चिह्नित करना असली type को उस function के अंदर runtime पर जांचने और उपयोग करने देता है।
  • reified सिर्फ inline functions पर अनुमति है, क्योंकि inlining ही हर call site पर असली type information को संरक्षित करता है।
  • Reified type parameters is T और T::class जैसे operations को संभव बनाते हैं जो आमतौर पर JVM type erasure की वजह से असंभव हैं।
  • एक आम उपयोग एक generic helper function है जो किसी collection को runtime-provided type से filter या जांचता है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.