← Back to Kotlin Course | Chapter 11: Generics | Lesson 2 of 6

Generic Functions क्या हैं

एक generic function एक recipe है जो आपके दिए किसी भी तरह के ingredient पर काम कर सकता है, हर call पर type ताज़ा पता लगाते हुए।
Syntax
kotlin
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

markup
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"))
}

Generic Functions on Collections

Generic functions किसी भी element type के collections के साथ काम करने के लिए खासतौर पर आम हैं, जैसे पहले दो items return करना चाहे वे किसी भी type के हों।

उदाहरण: Generic Functions on Collections

markup
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")))
}

Multiple Type Parameters in a Function

एक generic function एक से ज़्यादा type parameters declare कर सकता है, उन operations के लिए उपयोगी जो दो अलग तरह की values को एक result में combine करें।

उदाहरण: Multiple Type Parameters in a Function

markup
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))
}

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?

markup
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")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. type parameter को function name से पहले के बजाय बाद में रखना; सही order है fun <T> functionName(...)।
  2. यह मान लेना कि कोई generic function T पर कोई constraint restricting किए बिना type-specific operations (जैसे arithmetic) कर सकता है।
  3. किसी 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.