Type Inference क्या है
In this page:
val name = value // type inferred
val name: Type = value // explicit type
var name = value // type stays fixed
How Inference Works
जब आप val age = 25 लिखते हैं, Kotlin value 25 को देखता है और infer करता है कि age का type Int है, बिना आपके कहीं Int लिखे। यह compile time पर होता है, इसलिए type fixed है और उतनी ही सख्ती से जांचा जाता है जितना अगर आपने इसे लिखा होता।
उदाहरण: How Inference Works
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `age`, set to `25`
val age = 25
// Declare a read-only variable `price`, set to `19.99`
val price = 19.99
// Declare a read-only variable `name`, set to `"Kotlin"`
val name = "Kotlin"
// Print "$name costs $price for someone aged $age" to the console, with a trailing newline
println("$name costs $price for someone aged $age")
}
Login to try C/C++/Java/PHP code in the editor
Explicit Type Annotations
आप हमेशा colon के बाद explicitly type लिख सकते हैं, जैसे val age: Int = 25। यह तब ज़रूरी है जब infer करने के लिए कोई initial value न हो, और कुछ cases में readability भी सुधार सकता है।
उदाहरण: Explicit Type Annotations
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `age` (an Int), set to `25`
val age: Int = 25
// Declare a read-only variable `price` (a Double), set to `19.99`
val price: Double = 19.99
// Print "Age: $age, Price: $price" to the console, with a trailing newline
println("Age: $age, Price: $price")
}
Login to try C/C++/Java/PHP code in the editor
Inference and Static Typing
भले ही type हमेशा न लिखा जाए, Kotlin statically typed बना रहता है: एक बार infer होने के बाद, किसी variable का type बदल नहीं सकता, और mismatched type assign करना एक compile-time error है।
score = "high" करने की कोशिश compile नहीं होगी -- inferred type Int lock हो चुका है।उदाहरण: Inference and Static Typing
fun main() {
var score = 100 // inferred as Int
score = 105 // fine, still an Int
println("Score: $score")
}
Login to try C/C++/Java/PHP code in the editor
When Inference Is Not Possible
बिना किसी initial value के declared एक val या var के पास infer करने का कोई आधार नहीं, इसलिए उस स्थिति में एक explicit type चाहिए, और variable को उपयोग होने से पहले assign किया जाना चाहिए।
उदाहरण: When Inference Is Not Possible
// Entry point: execution of the program starts here
fun main() {
val result: Int
result = 10 * 4
// Print "Result: $result" to the console, with a trailing newline
println("Result: $result")
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि Kotlin dynamically typed है क्योंकि आप शायद ही कभी explicitly types लिखते हैं; Kotlin statically typed है, यह सिर्फ compile time पर types infer करता है।
- किसी
valको uninitialized छोड़ना और फिर भी Kotlin से type infer करने की उम्मीद करना; inference को काम करने के लिए एक initial value चाहिए। - आदत से हर जगह redundant explicit types जोड़ना, code को ज़रूरत से ज़्यादा verbose बनाते हुए।
- Kotlin किसी variable का type उसकी initial value से compile time पर infer करता है, runtime पर नहीं।
- Kotlin statically typed है -- हर variable का एक fixed type होता है, भले ही वह type explicitly न लिखा गया हो।
- Type inference को एक initializer चाहिए; बिना किसी value वाला अकेला
val xinfer नहीं हो सकता। - Explicit type annotations अब भी स्पष्टता के लिए या जब infer करने के लिए कोई initializer न हो तब उपयोगी हैं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: