Infix Functions क्या हैं
3 to 4, dots और parentheses उपयोग करने के बजाय।In this page:
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 होने देता है।
उदाहरण: What Makes a Function Infix
infix fun Int.timesRepeated(action: () -> Unit) {
repeat(this) { action() }
}
// Entry point: execution of the program starts here
fun main() {
3 timesRepeated { println("Hi!") }
}
Login to try C/C++/Java/PHP code in the editor
The Built-in to Infix Function
standard library का to function infix है, साफ़, readable syntax के साथ दो values से एक Pair बनाते हुए: "key" to "value"।
उदाहरण: The Built-in to Infix Function
// 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}")
}
Login to try C/C++/Java/PHP code in the editor
Boolean Infix Functions
and, or, और xor Boolean पर (और bitwise operations के लिए Int पर) infix functions हैं, logical combinations को स्वाभाविक रूप से पढ़ने देते हुए।
उदाहरण: Boolean Infix Functions
// 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}")
}
Login to try C/C++/Java/PHP code in the editor
Writing Your Own Infix Function
Custom infix functions छोटे domain-specific operations के लिए उपयोगी हैं जो दो values के बीच एक संबंध की तरह स्वाभाविक रूप से पढ़े जाते हैं, जैसे दो ranges को combine करना।
उदाहरण: Writing Your Own Infix Function
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)
}
Login to try C/C++/Java/PHP code in the editor
- एक से ज़्यादा required parameter वाले function को
infixचिह्नित करने की कोशिश करना; infix functions को बिल्कुल एक parameter लेना चाहिए। - यह भूल जाना कि infix functions को member या extension functions होना चाहिए, हर case में top-level standalone functions नहीं (वे हो सकते हैं, लेकिन इन्हें एक receiver चाहिए)।
- ऐसी चीज़ों के लिए custom infix functions का ज़्यादा उपयोग करना जो स्वाभाविक रूप से किसी operator-जैसे संबंध के रूप में नहीं पढ़ी जातीं, स्पष्टता को नुकसान पहुंचाते हुए।
- किसी function को
infixkeyword से चिह्नित करना इसे बिना 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: