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

Extension Properties क्या हैं

एक extension property आपको किसी मौजूदा type में एक नया labeled fact जोड़ने देता है, जब तक Kotlin इसे सीधे store करने के बजाय तुरंत compute कर सके।
Syntax
kotlin
val Type.propertyName: PropType
    get() = expression        // this is the receiver

var Type.writable: PropType
    get() = expression
    set(value) { /* update the receiver */ }

Declaring an Extension Property

एक extension property किसी मौजूदा type में एक computed property जोड़ता है, एक custom get() के साथ लिखा गया क्योंकि इसके लिए एक असली backing field store करने का कोई तरीका नहीं।

उदाहरण: Declaring an Extension Property

markup
val String.firstChar: Char
    get() = this[0]

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

Extension Properties Must Be Computed

चूंकि किसी मौजूदा class में असली storage जोड़ने के लिए कोई जगह नहीं, हर extension property को हर बार access होने पर receiver से अपनी value निकालनी चाहिए।

उदाहरण: Extension Properties Must Be Computed

markup
val List<Int>.average2: Double
    get() = if (this.isEmpty()) 0.0 else this.sum().toDouble() / this.size

// Entry point: execution of the program starts here
fun main() {
    // Print `listOf(2, 4, 6).average2` to the console, with a trailing newline
    println(listOf(2, 4, 6).average2)
}

Writable Extension Properties

एक var extension property get() और set(value) दोनों define कर सकती है, इसे एक computed read-write property की तरह व्यवहार करने देते हुए भले ही यह सीधे अपनी कोई state store न करे।

उदाहरण: Writable Extension Properties

markup
var StringBuilder.lastChar: Char
    get() = this[this.length - 1]
    set(value) {
        this.setCharAt(this.length - 1, value)
    }

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `sb`, set to `StringBuilder("Kotlin!")`
    val sb = StringBuilder("Kotlin!")
    sb.lastChar = '?'
    // Print `sb` to the console, with a trailing newline
    println(sb)
}

Extension Properties on Custom Types

Extension properties आपकी अपनी classes पर भी काम करती हैं, जो original class को modify किए बिना किसी object के data का एक derived, read-only view जोड़ने के लिए उपयोगी है।

उदाहरण: Extension Properties on Custom Types

markup
// Define a data class `Rectangle`
data class Rectangle(val width: Double, val height: Double)

val Rectangle.area: Double
    get() = width * height

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `rect`, set to `Rectangle(4.0, 5.0)`
    val rect = Rectangle(4.0, 5.0)
    // Print "Area: ${rect.area}" to the console, with a trailing newline
    println("Area: ${rect.area}")
}
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 property को एक backing field (एक initial stored value) देने की कोशिश करना; extension properties में backing fields नहीं हो सकते, सिर्फ computed getters (और setters)।
  2. यह भूल जाना कि एक extension var property को custom get() और set() दोनों चाहिए क्योंकि default लेने के लिए कोई field नहीं।
  3. महंगे computations के लिए extension properties का ज़्यादा उपयोग करना, यह भूलते हुए कि हर access value फिर से compute करता है क्योंकि कुछ भी cached नहीं होता।
चैप्टर सारांश
  • एक extension property val/var ReceiverType.propertyName: Type get() = ... के रूप में declared होती है।
  • Extension properties में एक backing field नहीं हो सकता -- इन्हें एक custom getter (और var के लिए setter) के ज़रिए compute होना चाहिए।
  • Extension functions की तरह, extension properties declared type के आधार पर statically resolved होती हैं।
  • Extension properties किसी मौजूदा type में एक derived, read-only fact जोड़ने का एक साफ़ तरीका हैं।
🔒

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.