Nothing Type क्या है
fun functionName(message: String): Nothing {
throw SomeException(message) // never returns normally
}
val value = nullable ?: throw SomeException()
What Nothing Represents
Nothing एक खास type है जिसकी बिल्कुल कोई values नहीं हैं। यह संकेत देता है कि कोई function कभी सामान्य रूप से return नहीं करेगा -- यह या तो एक exception फेंकता है या हमेशा के लिए loop करता है।
उदाहरण: What Nothing Represents
// Define the function `fail` taking `message` and returning a `Nothing`
fun fail(message: String): Nothing {
throw IllegalStateException(message)
}
// Entry point: execution of the program starts here
fun main() {
try {
fail("Something went wrong")
} catch (e: IllegalStateException) {
// Print "Caught: ${e.message}" to the console, with a trailing newline
println("Caught: ${e.message}")
}
}
Login to try C/C++/Java/PHP code in the editor
Nothing as a Subtype of Everything
चूंकि Nothing Kotlin में हर दूसरे type का subtype है, Nothing type का एक expression कहीं भी उपयोग किया जा सकता है जहां किसी भी type की value की उम्मीद हो, compiler को दोनों branches को consistently treat करने देते हुए।
उदाहरण: Nothing as a Subtype of Everything
// Define the function `getOrDefault` taking `value` and returning an Int
fun getOrDefault(value: Int?): Int {
// Return `value ?: throw IllegalArgumentException("value was null")` from this function
return value ?: throw IllegalArgumentException("value was null")
}
// Entry point: execution of the program starts here
fun main() {
// Print `getOrDefault(5)` to the console, with a trailing newline
println(getOrDefault(5))
}
Login to try C/C++/Java/PHP code in the editor
TODO() Returns Nothing
standard library के TODO() function का return type Nothing है और यह एक NotImplementedError फेंकता है, आपको compiler की type checks को satisfy करते हुए एक function body को stub out करने देते हुए।
उदाहरण: TODO() Returns Nothing
// Define the function `plannedFeature` with no parameters and returning an Int
fun plannedFeature(): Int {
// Return `try {` from this function
return try {
TODO("implement this later")
} catch (e: NotImplementedError) {
-1
}
}
// Entry point: execution of the program starts here
fun main() {
// Print "Placeholder result: ${plannedFeature()}" to the console, with a trailing newline
println("Placeholder result: ${plannedFeature()}")
}
Login to try C/C++/Java/PHP code in the editor
NothingकोnullयाUnitके साथ confuse करना;Nothingका मतलब है कि कोई function कभी सामान्य रूप से पूरा नहीं होता, जबकिUnitका मतलब है कि यह पूरा होता है लेकिन कोई उपयोगी value return नहीं करता।- यह न समझना कि
TODO()जैसा कोई functionNothingreturn करता है, यही वजह है कि इसे बिना type error के कहीं भी उपयोग किया जा सकता है जहां एक value की उम्मीद हो। Nothingका एक instance बनाने की कोशिश करना; इसकी कोई instances नहीं हैं -- यह सिर्फ उस code को describe करने के लिए मौजूद है जो कभी return नहीं करता।
Nothingबिना instances वाला एक type है, जो किसी ऐसे expression को represent करने के लिए उपयोग होता है जो कभी सामान्य रूप से पूरा नहीं होता।- जो functions हमेशा एक exception फेंकते हैं, जैसे
throwसे खत्म होने वाले, उनका inferred return typeNothingहोता है। - चूंकि
Nothingहर दूसरे type का subtype है, इसे कहीं भी उपयोग किया जा सकता है जहां कोई भी type की उम्मीद हो, जैसे Elvis operator में??के बाद। - standard library से
TODO()Nothingreturn करता है, आपको unfinished code को stub out करने देते हुए जो फिर भी type-check करता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: