← Back to Kotlin Course | Chapter 9: Lambdas & Higher-Order Functions | Lesson 4 of 6

it Keyword क्या है

it एक shortcut nickname है जो Kotlin अपने आप किसी lambda के इकलौते parameter को देता है, ताकि आपको इसे खुद नाम न देना पड़े।
Syntax
kotlin
list.map { it * 2 }              // it: single implicit parameter
list.map { item -> item * 2 }    // same, with an explicit name

Using it for a Single Parameter

जब कोई lambda बिल्कुल एक parameter लेता है, Kotlin आपको इसे नाम देने से बचने देता है और lambda body के अंदर इसे बस it के रूप में refer करने देता है।

उदाहरण: Using it for a Single Parameter

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `numbers`, set to `listOf(1, 2, 3, 4)`
    val numbers = listOf(1, 2, 3, 4)
    // Declare a read-only variable `doubled`, set to `numbers.map { it * 2 }`
    val doubled = numbers.map { it * 2 }
    // Print `doubled` to the console, with a trailing newline
    println(doubled)
}

Comparing it to an Explicit Name

वही lambda it के बजाय एक explicit parameter name के साथ लिखा जा सकता है; दोनों बराबर हैं, लेकिन लंबी bodies के लिए explicit names ज़्यादा साफ़ हैं।

उदाहरण: Comparing it to an Explicit Name

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `numbers`, set to `listOf(1, 2, 3, 4)`
    val numbers = listOf(1, 2, 3, 4)
    // Declare a read-only variable `doubledImplicit`, set to `numbers.map { it * 2 }`
    val doubledImplicit = numbers.map { it * 2 }
    // Declare a read-only variable `doubledExplicit`, set to `numbers.map { number -> number * 2 }`
    val doubledExplicit = numbers.map { number -> number * 2 }
    // Print "Implicit: $doubledImplicit" to the console, with a trailing newline
    println("Implicit: $doubledImplicit")
    // Print "Explicit: $doubledExplicit" to the console, with a trailing newline
    println("Explicit: $doubledExplicit")
}

Avoiding Confusing Nested it

जब lambdas nested हों और दोनों it उपयोग करें, inner it outer वाले को shadow कर देता है, जो भ्रामक हो सकता है -- कम से कम एक को explicitly नाम देना ambiguity से बचाता है।

उदाहरण: Avoiding Confusing Nested it

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `groups`, set to `listOf(listOf(1, 2), listOf(3, 4))`
    val groups = listOf(listOf(1, 2), listOf(3, 4))
    // Declare a read-only variable `result`, set to `groups.map { group -> group.map { it * 10 } }`
    val result = groups.map { group -> group.map { it * 10 } }
    // Print `result` to the console, with a trailing newline
    println(result)
}

When to Prefer a Named Parameter

multi-line या complex logic वाले lambdas के लिए, parameter को it के बजाय एक descriptive name देना आमतौर पर intent को भविष्य के readers के लिए कहीं स्पष्ट बना देता है।

उदाहरण: When to Prefer a Named Parameter

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `prices`, set to `listOf(10, 25, 40)`
    val prices = listOf(10, 25, 40)
    // Declare a read-only variable `withTax`, set to `prices.map { price ->`
    val withTax = prices.map { price ->
        // Declare a read-only variable `tax`, set to `price * 0.1`
        val tax = price * 0.1
        price + tax
    }
    // Print `withTax` to the console, with a trailing newline
    println(withTax)
}
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. किसी nested lambda के अंदर it उपयोग करना जहां एक outer it भी scope में हो, भ्रामक shadowing का कारण बनते हुए; parameter को explicitly नाम देना इससे बचाता है।
  2. एक से ज़्यादा parameter लेने वाले lambda में it उपयोग करने की कोशिश करना; it सिर्फ बिल्कुल एक implicit parameter के लिए काम करता है।
  3. लंबे या जटिल lambdas में it का ज़्यादा उपयोग करना जहां एक named parameter code को पढ़ना कहीं ज़्यादा आसान बना देता।
चैप्टर सारांश
  • जब किसी lambda में बिल्कुल एक parameter हो और इसका type infer किया जा सके, उस parameter को implicitly it के रूप में refer किया जा सकता है।
  • it सिर्फ single-parameter lambdas पर लागू होता है; multi-parameter lambdas को अपने सभी parameters explicitly नाम देने चाहिए।
  • दोनों implicit it पर निर्भर nested lambdas एक-दूसरे को भ्रामक रूप से shadow कर सकते हैं, इसलिए कम से कम outer वाले को नाम देना ज़्यादा सुरक्षित है।
  • लंबे या ज़्यादा जटिल lambda bodies के लिए, parameter को explicitly नाम देना आमतौर पर it पर निर्भर रहने से readability सुधारता है।
🔒

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.