Safe Call Operator क्या है
In this page:
val result = nullable?.property
val result = nullable?.method()
val result = a?.b?.c // null if any link is null
Basic Safe Call
?. सिर्फ तभी एक method या property call करता है जब receiver null न हो; अगर receiver null हो, तो पूरा expression बिना throw किए null में short-circuit हो जाता है।
उदाहरण: Basic Safe Call
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `name` (a `String?`), set to `"Kotlin"`
val name: String? = "Kotlin"
// Declare a read-only variable `nullName` (a `String?`), set to `null`
val nullName: String? = null
// Print `name?.length` to the console, with a trailing newline
println(name?.length)
// Print `nullName?.length` to the console, with a trailing newline
println(nullName?.length)
}
Login to try C/C++/Java/PHP code in the editor
Chaining Safe Calls
कई ?. operators को साथ chain किया जा सकता है, जैसे user?.address?.city, और पूरी chain जैसे ही इसका कोई हिस्सा null हो null में evaluate हो जाती है।
उदाहरण: Chaining Safe Calls
// Define a data class `Address`
data class Address(val city: String)
// Define a data class `Person`
data class Person(val name: String, val address: Address?)
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `personWithAddress`, set to `Person("Ana", Address("Berlin"))`
val personWithAddress = Person("Ana", Address("Berlin"))
// Declare a read-only variable `personWithoutAddress`, set to `Person("Bo", null)`
val personWithoutAddress = Person("Bo", null)
// Print `personWithAddress.address?.city` to the console, with a trailing newline
println(personWithAddress.address?.city)
// Print `personWithoutAddress.address?.city` to the console, with a trailing newline
println(personWithoutAddress.address?.city)
}
Login to try C/C++/Java/PHP code in the editor
Safe Calls with Function Calls
एक safe call किसी function को call करने से पहले भी काम करता है, इसलिए text?.trim() सिर्फ तभी trim() चलाता है जब text null न हो, अन्यथा null produce करते हुए।
उदाहरण: Safe Calls with Function Calls
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `input` (a `String?`), set to `" hello "`
val input: String? = " hello "
// Declare a read-only variable `trimmed`, set to `input?.trim()`
val trimmed = input?.trim()
// Print "Trimmed: $trimmed" to the console, with a trailing newline
println("Trimmed: $trimmed")
}
Login to try C/C++/Java/PHP code in the editor
Safe Calls in Conditions
Safe calls equality checks के साथ स्वाभाविक रूप से combine होते हैं, जैसे if (text?.length == 0), क्योंकि null की किसी Int से तुलना करना throw करने के बजाय बस false evaluate होता है।
उदाहरण: Safe Calls in Conditions
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `text` (a `String?`), set to `null`
val text: String? = null
// Check whether `text?.length == 0`
if (text?.length == 0) {
// Print "Empty string" to the console, with a trailing newline
println("Empty string")
// Otherwise, run this branch
} else {
// Print "Either not empty or null" to the console, with a trailing newline
println("Either not empty or null")
}
}
Login to try C/C++/Java/PHP code in the editor
- किसी nullable receiver पर
?.के बजाय.उपयोग करना, जिसे compiler सीधे reject कर देता है। - कई
?.calls को chain करना लेकिन यह भूल जाना कि पूरी chain जैसे ही कोई linknullहोnullमें short-circuit हो जाती है। - यह मान लेना कि
?.nullपर एक exception फेंकता है; यह कभी throw नहीं करता, यह बस इसके बजायnullevaluate होता है।
?.सिर्फ तभी एक member call करता है जब receivernullन हो; नहीं तो पूरा expressionnullevaluate होता है।a?.b?.cजैसी chained safe calls जैसे ही chain में कोई linknullहोnullमें short-circuit हो जाती हैं।?.कभी कोई exception नहीं फेंकता -- यह किसी nullable type पर साधारण.का सुरक्षित विकल्प है।- किसी safe call का result हमेशा एक nullable type होता है, क्योंकि यह
nullहो सकता है।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: