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

Infix Extension Functions क्या हैं

infix को extensions के साथ combine करना आपको किसी भी मौजूदा type के लिए अपने खुद के natural-पढ़े जाने वाले operators invent करने देता है, जैसे दो values के बीच रखा एक custom word।
Syntax
kotlin
infix fun ReceiverType.functionName(param: Type): ReturnType {
    return value
}

val result = receiver functionName argument

Writing an Infix Extension

infix को fun ReceiverType.name(param: Type) के साथ combine करना किसी मौजूदा type में एक operator-जैसी call syntax जोड़ता है, receiver name argument के रूप में उपयोग करने योग्य।

उदाहरण: Writing an Infix Extension

markup
infix fun Int.plusPercent(percent: Int): Int = this + (this * percent / 100)

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `price`, set to `100`
    val price = 100
    // Print "Price with 20% added: ${price plusPercent 20}" to the console, with a trailing newline
    println("Price with 20% added: ${price plusPercent 20}")
}

Building Readable Domain Logic

Infix extensions अक्सर domain-specific operations को एक natural, sentence-जैसी पढ़ाई देने के लिए उपयोग होते हैं, जो specialized code को एक साधारण method call से कहीं साफ़ बना सकता है।

उदाहरण: Building Readable Domain Logic

markup
infix fun Int.daysAfter(startDay: Int): Int = (startDay + this) % 7

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `monday`, set to `1`
    val monday = 1
    // Print "3 days after Monday: ${3 daysAfter monday}" to the console, with a trailing newline
    println("3 days after Monday: ${3 daysAfter monday}")
}

Infix Extensions on Custom Types

Infix extensions आपकी अपनी classes पर भी काम करते हैं, आपके control वाले types के instances के बीच natural-पढ़े जाने वाले operations define करने देते हुए।

उदाहरण: Infix Extensions on Custom Types

markup
// Define a data class `Vector`
data class Vector(val x: Int, val y: Int)

infix fun Vector.dot(other: Vector): Int = x * other.x + y * other.y

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `v1`, set to `Vector(2, 3)`
    val v1 = Vector(2, 3)
    // Declare a read-only variable `v2`, set to `Vector(4, 5)`
    val v2 = Vector(4, 5)
    // Print "Dot product: ${v1 dot v2}" to the console, with a trailing newline
    println("Dot product: ${v1 dot v2}")
}

When to Avoid Infix Style

Infix notation उन operations के लिए सबसे अच्छा पढ़ा जाता है जो दो values के बीच किसी natural verb या relationship जैसी हों; बाकी किसी भी चीज़ के लिए, एक साफ़ नाम वाला regular method call ज़्यादा उपयुक्त है।

Note: अगर व्यक्त किया जा रहा संबंध सिर्फ शब्दों से स्पष्ट नहीं है तो infix style पर combineWith() जैसे regular method name को प्राथमिकता दें।

उदाहरण: When to Avoid Infix Style

markup
infix fun String.repeatedTwice(separator: String): String = "$this$separator$this"

// Entry point: execution of the program starts here
fun main() {
    // Print "Hi" repeatedTwice ", " to the console, with a trailing newline
    println("Hi" repeatedTwice ", ")
}
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. यह भूल जाना कि एक infix extension function को अब भी बिल्कुल एक parameter लेना चाहिए, किसी दूसरे infix function जैसा ही नियम।
  2. infix extension notation को ऐसे operations पर लागू करना जो स्वाभाविक रूप से symmetric या relational नहीं हैं, readability को मदद करने के बजाय नुकसान पहुंचाते हुए।
  3. extension function को infix चिह्नित न करना, फिर confuse होना कि operator-जैसी call syntax a op b compile क्यों नहीं होती।
चैप्टर सारांश
  • infix को किसी extension function के साथ combine करना किसी मौजूदा type को एक बिल्कुल नई operator-जैसी syntax देता है।
  • Extension को अब भी infix की ज़रूरत पूरी करनी चाहिए कि बिल्कुल एक parameter हो।
  • Infix extensions आमतौर पर छोटे domain-specific languages (DSLs) बनाने के लिए उपयोग होते हैं जो स्वाभाविक रूप से पढ़े जाते हैं।
  • किसी भी infix function की तरह, इस style को उन operations के लिए रखा जाना चाहिए जो genuinely एक natural relation जैसे पढ़े जाएं।
🔒

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.