Labels के साथ Return
In this page:
collection.forEach {
return@forEach // return from the lambda only
}
collection.forEach label@{
return@label
}
Non-Local Return from a Lambda
forEach जैसे किसी inline function को पास किए lambda के अंदर एक साधारण return सिर्फ उस एक iteration को skip नहीं करता -- यह तुरंत पूरे enclosing function से बाहर निकल जाता है।
उदाहरण: Non-Local Return from a Lambda
// Define the function `findFirstEven` taking `numbers` and returning a `Int?`
fun findFirstEven(numbers: List<Int>): Int? {
numbers.forEach {
if (it % 2 == 0) return it
}
// Return `null` from this function
return null
}
// Entry point: execution of the program starts here
fun main() {
// Print "First even: ${findFirstEven(listOf(1, 3, 4, 5))}" to the console, with a trailing newline
println("First even: ${findFirstEven(listOf(1, 3, 4, 5))}")
}
Login to try C/C++/Java/PHP code in the editor
Returning from the Lambda Only
जिस function को lambda पास किया गया है उसके नाम पर बने implicit label का उपयोग करना, जैसे return@forEach, सिर्फ उस single lambda invocation से return करता है और loop को अगले item के साथ जारी रहने देता है।
उदाहरण: Returning from the Lambda Only
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `numbers`, set to `listOf(1, 2, 3, 4, 5)`
val numbers = listOf(1, 2, 3, 4, 5)
numbers.forEach {
if (it % 2 == 0) return@forEach
// Print "Odd: $it" to the console, with a trailing newline
println("Odd: $it")
}
}
Login to try C/C++/Java/PHP code in the editor
Explicit Labels on a Lambda
आप किसी lambda से पहले अपना खुद का explicit label भी define कर सकते हैं, जैसे myLoop@ { ... }, और specifically return@myLoop का उपयोग करके उससे return कर सकते हैं।
उदाहरण: Explicit Labels on a Lambda
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `numbers`, set to `listOf(1, 2, 3, 4, 5)`
val numbers = listOf(1, 2, 3, 4, 5)
numbers.forEach myLoop@{
if (it == 3) return@myLoop
// Print "Value: $it" to the console, with a trailing newline
println("Value: $it")
}
}
Login to try C/C++/Java/PHP code in the editor
Choosing Local vs Non-Local Return
यह तय करना कि किसी lambda के अंदर return को पूरे function से बाहर निकलना चाहिए या सिर्फ उस lambda invocation से, एक सोचा-समझा choice है -- जब भी सिर्फ lambda को रुकना चाहिए तब labeled form का उपयोग करें।
उदाहरण: Choosing Local vs Non-Local Return
// Define the function `processAll` taking `numbers`
fun processAll(numbers: List<Int>) {
// Loop over `numbers`, binding each item to `n`
for (n in numbers) {
numbers.forEach {
if (it == n) return@forEach
// Print "Comparing $n with $it" to the console, with a trailing newline
println("Comparing $n with $it")
}
}
}
// Entry point: execution of the program starts here
fun main() {
processAll(listOf(1, 2))
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि
forEachको पास किए किसी lambda के अंदर एक साधारणreturnसिर्फ उस lambda से बाहर निकलता है, जबकि यह असल में enclosing function से return करता है (एक non-local return)। - जब सिर्फ lambda को ही रुकना चाहिए तब return को label करना भूल जाना, जिससे surrounding function अनपेक्षित रूप से जल्दी exit हो जाता है।
- जब precision मायने रखे तब ज़्यादा स्पष्ट explicit label syntax के बजाय implicit labels (function के नाम पर, जैसे
forEach$0) का उपयोग करना।
- किसी inline function को पास किए lambda के अंदर एक साधारण
returnएक non-local return करता है, enclosing function से पूरी तरह बाहर निकलते हुए। - किसी lambda को
name@ { ... }से label करना औरreturn@nameउपयोग करना सिर्फ उस lambda से return करता है, पूरे function से नहीं। - Kotlin उस function के नाम से मेल खाता एक implicit label भी प्रदान करता है जिसे lambda पास किया गया है, जैसे
return@forEach। - सही तरह का return (local बनाम non-local) चुनना higher-order functions से बने loops में अचानक control-flow bugs से बचाता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: