Extension Functions क्या हैं
In this page:
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
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())
}
Login to try C/C++/Java/PHP code in the editor
Using this Inside an Extension
किसी extension function के body के अंदर, this receiver को refer करता है -- वह value जिस पर extension call किया गया था -- और आमतौर पर इसे एक regular member function की तरह छोड़ा जा सकता है।
उदाहरण: Using this Inside an Extension
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()}")
}
Login to try C/C++/Java/PHP code in the editor
Extensions on Standard Library Types
Extension functions उन types को सुविधाजनक behavior जोड़ने के लिए खासतौर पर सुविधाजनक हैं जिन्हें आप सीधे modify नहीं कर सकते, standard library या third-party classes सहित।
उदाहरण: Extensions on Standard Library Types
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())
}
Login to try C/C++/Java/PHP code in the editor
Static Resolution of Extensions
कौन सा extension function call होता है यह compile time पर expression के declared (static) type के आधार पर तय होता है, object के असली runtime type के आधार पर नहीं -- overridden member functions के विपरीत।
animal statically Animal typed है, भले ही यह runtime पर एक Dog रखता हो।उदाहरण: Static Resolution of Extensions
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())
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि कोई extension function असल में original class को modify कर देता है; यह statically resolved होता है और किसी भी तरह से class को खुद नहीं बदलता।
- यह उम्मीद करना कि extension functions member functions की तरह polymorphic हों; कौन सा extension call होता है यह compile time पर declared type से तय होता है, runtime type से नहीं।
- किसी दूसरे 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: