← Back to Kotlin Course | Chapter 10: Extension Functions & Properties | Lesson 1 of 6

Extension Functions क्या हैं

एक extension function आपको किसी ऐसे type पर एक बिल्कुल नई ability जोड़ने देता है जिसके आप मालिक भी नहीं हैं, जैसे किसी और के remote control पर एक नया button जोड़ना।
Syntax
kotlin
fun Type.functionName(param: Type): ReturnType {
    return value   // this refers to the receiver
}

value.functionName(argument)

Declaring an Extension Function

एक extension function fun Type.name(...) के रूप में लिखा जाता है, आपको value.name(...) को इस तरह call करने देते हुए जैसे name Type का एक असली member हो, भले ही Type के अपने source code को कभी छुआ न गया हो।

उदाहरण: Declaring an Extension Function

markup
fun String.shout(): String = this.toUpperCase() + "!"

// Entry point: execution of the program starts here
fun main() {
    // Print `"hello".shout()` to the console, with a trailing newline
    println("hello".shout())
}

Using this Inside an Extension

किसी extension function के body के अंदर, this receiver को refer करता है -- वह value जिस पर extension call किया गया था -- और आमतौर पर इसे एक regular member function की तरह छोड़ा जा सकता है।

उदाहरण: Using this Inside an Extension

markup
fun Int.isEven(): Boolean = this % 2 == 0

// Entry point: execution of the program starts here
fun main() {
    // Print "4 is even: ${4.isEven()}" to the console, with a trailing newline
    println("4 is even: ${4.isEven()}")
    // Print "7 is even: ${7.isEven()}" to the console, with a trailing newline
    println("7 is even: ${7.isEven()}")
}

Extensions on Standard Library Types

Extension functions उन types को सुविधाजनक behavior जोड़ने के लिए खासतौर पर सुविधाजनक हैं जिन्हें आप सीधे modify नहीं कर सकते, standard library या third-party classes सहित।

उदाहरण: Extensions on Standard Library Types

markup
fun List<Int>.secondOrNull(): Int? = if (this.size >= 2) this[1] else null

// Entry point: execution of the program starts here
fun main() {
    // Print `listOf(10, 20, 30).secondOrNull()` to the console, with a trailing newline
    println(listOf(10, 20, 30).secondOrNull())
    // Print `listOf(10).secondOrNull()` to the console, with a trailing newline
    println(listOf(10).secondOrNull())
}

Static Resolution of Extensions

कौन सा extension function call होता है यह compile time पर expression के declared (static) type के आधार पर तय होता है, object के असली runtime type के आधार पर नहीं -- overridden member functions के विपरीत।

Note: यह "An animal" print करता है क्योंकि animal statically Animal typed है, भले ही यह runtime पर एक Dog रखता हो।

उदाहरण: Static Resolution of Extensions

markup
open class Animal
// Define a class `Dog`
class Dog : Animal()

fun Animal.describe() = "An animal"
fun Dog.describe() = "A dog"

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `animal` (a `Animal`), set to `Dog()`
    val animal: Animal = Dog()
    // Print `animal.describe()` to the console, with a trailing newline
    println(animal.describe())
}
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. यह मान लेना कि कोई extension function असल में original class को modify कर देता है; यह statically resolved होता है और किसी भी तरह से class को खुद नहीं बदलता।
  2. यह उम्मीद करना कि extension functions member functions की तरह polymorphic हों; कौन सा extension call होता है यह compile time पर declared type से तय होता है, runtime type से नहीं।
  3. किसी दूसरे package में declared extension function को import करना भूल जाना, क्योंकि इसे किसी भी दूसरे top-level function की तरह import करना चाहिए।
चैप्टर सारांश
  • एक extension function fun ReceiverType.functionName(...) { ... } के रूप में declared होता है, किसी मौजूदा type में एक callable method जोड़ते हुए।
  • extension के अंदर, this उस receiver instance को refer करता है जिस पर function call किया गया था।
  • Extension functions declared type के आधार पर statically resolved होते हैं, असली runtime type के आधार पर dynamically नहीं।
  • Extension functions असल में original class को modify नहीं करते; वे सिर्फ किसी top-level function call के लिए syntactic sugar हैं।
🔒

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.