it Keyword क्या है
In this page:
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
// 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)
}
Login to try C/C++/Java/PHP code in the editor
Comparing it to an Explicit Name
वही lambda it के बजाय एक explicit parameter name के साथ लिखा जा सकता है; दोनों बराबर हैं, लेकिन लंबी bodies के लिए explicit names ज़्यादा साफ़ हैं।
उदाहरण: Comparing it to an Explicit Name
// 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")
}
Login to try C/C++/Java/PHP code in the editor
Avoiding Confusing Nested it
जब lambdas nested हों और दोनों it उपयोग करें, inner it outer वाले को shadow कर देता है, जो भ्रामक हो सकता है -- कम से कम एक को explicitly नाम देना ambiguity से बचाता है।
उदाहरण: Avoiding Confusing Nested it
// 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)
}
Login to try C/C++/Java/PHP code in the editor
When to Prefer a Named Parameter
multi-line या complex logic वाले lambdas के लिए, parameter को it के बजाय एक descriptive name देना आमतौर पर intent को भविष्य के readers के लिए कहीं स्पष्ट बना देता है।
उदाहरण: When to Prefer a Named Parameter
// 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)
}
Login to try C/C++/Java/PHP code in the editor
- किसी nested lambda के अंदर
itउपयोग करना जहां एक outeritभी scope में हो, भ्रामक shadowing का कारण बनते हुए; parameter को explicitly नाम देना इससे बचाता है। - एक से ज़्यादा parameter लेने वाले lambda में
itउपयोग करने की कोशिश करना;itसिर्फ बिल्कुल एक implicit parameter के लिए काम करता है। - लंबे या जटिल 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: