Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
An infix function in Kotlin is a member or extension function marked with the infix modifier, enabling it to be invoked using infix notation. This notation omits the dot (.) receiver call and the parentheses () around the argument, placing the function identifier directly between the receiver and the argument.
Technical Requirements
The Kotlin compiler enforces strict structural rules for a function to qualify for the infix modifier. The function must:
- Scope: Be declared as either a member function or an extension function.
- Arity: Declare exactly one value parameter.
- Parameter Constraints: The single parameter must not accept a variable number of arguments (
vararg) and must not declare a default value.
Syntax and Declaration
To define an infix function, prepend the infix keyword to the fun declaration.
// Extension infix function
infix fun String.append(other: String): String {
return this + other
}
class Container(val count: Int) {
// Member infix function
infix fun combine(other: Container): Container {
return Container(this.count + other.count)
}
}
Invocation Mechanics
When invoking an infix function, the compiler translates the infix syntax into a standard method dispatch.
val strResult = "System" append "Failure"
// Compiler translates to: "System".append("Failure")
val containerA = Container(5)
val containerB = Container(10)
val combined = containerA combine containerB
// Compiler translates to: containerA.combine(containerB)
Implicit Receivers:
If an infix function is called on the current class instance (this), the receiver cannot be implicit when using infix notation. The this keyword must be explicitly stated.
class Processor {
infix fun process(data: String) { /*...*/ }
fun execute() {
this process "Payload" // Valid
process("Payload") // Valid (Standard invocation)
// process "Payload" // Compilation Error: Receiver required
}
}
Operator Precedence
Infix function calls occupy a specific tier in Kotlin’s operator precedence hierarchy. They possess:
- Lower precedence than arithmetic operators, type casts, and the
rangeTo operator.
// ‘shl’ (shift left) is a standard library infix function
val result = 1 shl 2 + 3
// Evaluates as: 1 shl (2 + 3) -> 1 shl 5
* **Higher precedence** than boolean operators (`&&`, `||`), `is` and `in` checks, and other logic operators.
```kotlin
// 'xor' is a standard library infix function
val flag = true && false xor true
// Evaluates as: true && (false xor true)
- Left-associativity when chaining multiple infix functions of the same precedence.
val chain = “A” append “B” append “C”
// Evaluates as: (“A” append “B”) append “C”
<div style={{
display: "flex",
justifyContent: "space-between",
alignItems: "center",
maxWidth: "754px",
padding: "1rem 0",
marginBottom: "24px"
}}>
<span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Master Kotlin with Deep Grasping Methodology!</span>
<a
href="https://syntblaze.com"
target="_blank"
style={{
textDecoration: "none",
backgroundColor: "#007AFF",
color: "#ffffff",
padding: "6px 16px",
borderRadius: "16px",
fontSize: "0.9rem",
fontWeight: "600",
textAlign: "center",
transition: "background-color 0.2s ease"
}}
>
Learn More
</a>
</div>
<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
<img src="/images/skill-tracking.png" style={{ width: "30%", minWidth: 60 }} />
<img src="/images/nuggets.png" style={{ width: "30%", minWidth: 60 }} />
<img src="/images/bite-sized-exercises.png" style={{ width: "30%", minWidth: 60 }} />
</div>
<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
<img src="/images/mastery-chain.png" style={{ width: "30%", minWidth: 60 }} />
<img src="/images/element-previews.png" style={{ width: "30%", minWidth: 60 }} />
<img src="/images/element-explanations.png" style={{ width: "30%", minWidth: 60 }} />
</div>