← Back to Kotlin Course | Chapter 4: Functions | Lesson 6 of 7

Infix Functions क्या हैं

एक infix function आपको किसी function को दो values के बीच लिखकर call करने देता है, जैसे 3 to 4, dots और parentheses उपयोग करने के बजाय।
Syntax
kotlin
infix fun Type.functionName(other: Type): ReturnType {
    return value
}

val result = receiver functionName argument
val pair = key to value

What Makes a Function Infix

fun से पहले infix keyword जोड़ना किसी function को receiver और single argument के बीच उसका नाम रखकर, बिना किसी dot या parentheses के, call होने देता है।

Note: Infix functions को बिल्कुल एक parameter लेना चाहिए।

उदाहरण: What Makes a Function Infix

markup
infix fun Int.timesRepeated(action: () -> Unit) {
    repeat(this) { action() }
}

// Entry point: execution of the program starts here
fun main() {
    3 timesRepeated { println("Hi!") }
}

The Built-in to Infix Function

standard library का to function infix है, साफ़, readable syntax के साथ दो values से एक Pair बनाते हुए: "key" to "value"।

उदाहरण: The Built-in to Infix Function

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `entry`, set to `"name" to "Kotlin"`
    val entry = "name" to "Kotlin"
    // Print "${entry.first} = ${entry.second}" to the console, with a trailing newline
    println("${entry.first} = ${entry.second}")
}

Boolean Infix Functions

and, or, और xor Boolean पर (और bitwise operations के लिए Int पर) infix functions हैं, logical combinations को स्वाभाविक रूप से पढ़ने देते हुए।

उदाहरण: Boolean Infix Functions

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `a`, set to `true`
    val a = true
    // Declare a read-only variable `b`, set to `false`
    val b = false
    // Print "a and b = ${a and b}" to the console, with a trailing newline
    println("a and b = ${a and b}")
    // Print "a or b = ${a or b}" to the console, with a trailing newline
    println("a or b = ${a or b}")
    // Print "a xor b = ${a xor b}" to the console, with a trailing newline
    println("a xor b = ${a xor b}")
}

Writing Your Own Infix Function

Custom infix functions छोटे domain-specific operations के लिए उपयोगी हैं जो दो values के बीच एक संबंध की तरह स्वाभाविक रूप से पढ़े जाते हैं, जैसे दो ranges को combine करना।

Note: Infix notation को उन operations के लिए रखें जो genuinely किसी natural verb या relation जैसे पढ़े जाएं -- इसका ज़्यादा उपयोग एक normal function call को अस्पष्ट बना सकता है।

उदाहरण: Writing Your Own Infix Function

markup
infix fun Int.upTo(end: Int): List<Int> = (this..end).toList()

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `range`, set to `1 upTo 5`
    val range = 1 upTo 5
    // Print `range` to the console, with a trailing newline
    println(range)
}
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. एक से ज़्यादा required parameter वाले function को infix चिह्नित करने की कोशिश करना; infix functions को बिल्कुल एक parameter लेना चाहिए।
  2. यह भूल जाना कि infix functions को member या extension functions होना चाहिए, हर case में top-level standalone functions नहीं (वे हो सकते हैं, लेकिन इन्हें एक receiver चाहिए)।
  3. ऐसी चीज़ों के लिए custom infix functions का ज़्यादा उपयोग करना जो स्वाभाविक रूप से किसी operator-जैसे संबंध के रूप में नहीं पढ़ी जातीं, स्पष्टता को नुकसान पहुंचाते हुए।
चैप्टर सारांश
  • किसी function को infix keyword से चिह्नित करना इसे बिना dot या parentheses के call होने देता है, जैसे a to b।
  • एक infix function को बिल्कुल एक parameter होना चाहिए और एक member या extension function होना चाहिए।
  • Boolean/Int पर to (Pair बनाते हुए), and, or, और xor आम standard library infix functions हैं।
  • Infix notation को उन operations के लिए रखा जाना चाहिए जो दो चीज़ों के बीच किसी verb या relation की तरह स्वाभाविक रूप से पढ़े जाएं।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 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.