TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
-- operator in Kotlin is the unary decrement operator. It decreases the value of its operand by one. At compile time, Kotlin translates the -- operator into a call to the dec() operator function, followed by a reassignment to the original operand.
The operator operates in two distinct evaluation modes depending on its position relative to the operand:
Prefix Decrement (--a)
The operand is decremented first, and the expression evaluates to the new decremented value.
Postfix Decrement (a--)
The expression evaluates to the original value of the operand, and the operand is decremented immediately afterward.
Compiler Translation
Because Kotlin supports operator overloading, the-- operator is not limited to primitive numeric types. When the compiler encounters --a or a--, it resolves the operation to specific underlying execution steps to preserve the correct evaluation state.
Prefix Translation (--a)
a--)
Operator Overloading for Custom Types
To enable the-- operator on a custom class, you must define an operator fun dec() that returns a value of the same type (or a subtype). The compiler automatically handles the reassignment of the returned value back to the operand.
- The operand must be an expression that is valid on the left-hand side of an assignment. This includes mutable local variables (
var), mutable properties of objects (obj.property--), and indexed access expressions where thegetoperator returns a non-nullable type (e.g.,array[0]--). - The
dec()function must return a value. It should not mutate the object in place and returnUnit, as the compiler explicitly reassigns the result ofdec()to the original operand reference. - Nullable types do not possess a
dec()operator. Consequently, applying--to an indexed access expression that returns a nullable type (such asmap[key]--on a standardMutableMap) will result in a compilation error.
Master Kotlin with Deep Grasping Methodology!Learn More





