Generic Functions क्या हैं
In this page:
fun <T> functionName(param: T): T = param
fun <A, B> functionName(a: A, b: B): ReturnType = expression
functionName(value)
functionName<Type>(value)
Declaring a Generic Function
type parameter function name से ठीक पहले angle brackets में जाता है, function के parameters और return type सभी को उसी placeholder type को refer करने देते हुए।
उदाहरण: Declaring a Generic Function
fun <T> identity(value: T): T = value
// Entry point: execution of the program starts here
fun main() {
// Print `identity(42)` to the console, with a trailing newline
println(identity(42))
// Print `identity("Kotlin")` to the console, with a trailing newline
println(identity("Kotlin"))
}
Login to try C/C++/Java/PHP code in the editor
Generic Functions on Collections
Generic functions किसी भी element type के collections के साथ काम करने के लिए खासतौर पर आम हैं, जैसे पहले दो items return करना चाहे वे किसी भी type के हों।
उदाहरण: Generic Functions on Collections
fun <T> firstTwo(items: List<T>): List<T> = items.take(2)
// Entry point: execution of the program starts here
fun main() {
// Print `firstTwo(listOf(1, 2, 3, 4))` to the console, with a trailing newline
println(firstTwo(listOf(1, 2, 3, 4)))
// Print `firstTwo(listOf("a", "b", "c"))` to the console, with a trailing newline
println(firstTwo(listOf("a", "b", "c")))
}
Login to try C/C++/Java/PHP code in the editor
Multiple Type Parameters in a Function
एक generic function एक से ज़्यादा type parameters declare कर सकता है, उन operations के लिए उपयोगी जो दो अलग तरह की values को एक result में combine करें।
उदाहरण: Multiple Type Parameters in a Function
fun <A, B> combine(a: A, b: B): String = "$a and $b"
// Entry point: execution of the program starts here
fun main() {
// Print `combine(1, "one")` to the console, with a trailing newline
println(combine(1, "one"))
// Print `combine(true, 3.14)` to the console, with a trailing newline
println(combine(true, 3.14))
}
Login to try C/C++/Java/PHP code in the editor
Without a Constraint, T Is Treated as Any?
बिना कोई upper bound specify किए, T Any? की तरह व्यवहार करता है, इसलिए function सिर्फ वे चीज़ें कर सकता है जो बिल्कुल किसी भी type के लिए valid हों, जैसे store करना, return करना, या equality के लिए compare करना।
उदाहरण: Without a Constraint, T Is Treated as Any?
fun <T> printTwice(value: T) {
// Print `value` to the console, with a trailing newline
println(value)
// Print `value` to the console, with a trailing newline
println(value)
}
// Entry point: execution of the program starts here
fun main() {
printTwice(100)
printTwice("hello")
}
Login to try C/C++/Java/PHP code in the editor
- type parameter को function name से पहले के बजाय बाद में रखना; सही order है
fun <T> functionName(...)। - यह मान लेना कि कोई generic function
Tपर कोई constraint restricting किए बिना type-specific operations (जैसे arithmetic) कर सकता है। - किसी enclosing class पर पहले से मौजूद type parameter को फिर से declare करना जब इसकी उस particular function के लिए असल में ज़रूरत न हो।
- एक generic function function name से पहले अपना type parameter declare करता है:
fun <T> functionName(...)। - type parameter function की parameter list, return type, या body में दिखाई दे सकता है।
- बिना constraints के,
TकोAny?माना जाता है, इसलिए सिर्फ किसी भी type के लिए valid operations (जैसे इसे store या return करना) की अनुमति है। - Generic functions एक implementation को कई अलग types में सही और सुरक्षित रूप से काम करने देते हैं।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: