Infix Extension Functions क्या हैं
In this page:
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
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}")
}
Login to try C/C++/Java/PHP code in the editor
Building Readable Domain Logic
Infix extensions अक्सर domain-specific operations को एक natural, sentence-जैसी पढ़ाई देने के लिए उपयोग होते हैं, जो specialized code को एक साधारण method call से कहीं साफ़ बना सकता है।
उदाहरण: Building Readable Domain Logic
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}")
}
Login to try C/C++/Java/PHP code in the editor
Infix Extensions on Custom Types
Infix extensions आपकी अपनी classes पर भी काम करते हैं, आपके control वाले types के instances के बीच natural-पढ़े जाने वाले operations define करने देते हुए।
उदाहरण: Infix Extensions on Custom Types
// 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}")
}
Login to try C/C++/Java/PHP code in the editor
When to Avoid Infix Style
Infix notation उन operations के लिए सबसे अच्छा पढ़ा जाता है जो दो values के बीच किसी natural verb या relationship जैसी हों; बाकी किसी भी चीज़ के लिए, एक साफ़ नाम वाला regular method call ज़्यादा उपयुक्त है।
combineWith() जैसे regular method name को प्राथमिकता दें।उदाहरण: When to Avoid Infix Style
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 ", ")
}
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि एक infix extension function को अब भी बिल्कुल एक parameter लेना चाहिए, किसी दूसरे infix function जैसा ही नियम।
- infix extension notation को ऐसे operations पर लागू करना जो स्वाभाविक रूप से symmetric या relational नहीं हैं, readability को मदद करने के बजाय नुकसान पहुंचाते हुए।
- extension function को
infixचिह्नित न करना, फिर confuse होना कि operator-जैसी call syntaxa op bcompile क्यों नहीं होती।
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: