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.
*= (multiplication assignment) operator is a compound assignment operator that multiplies the value of the left-hand operand by the value of the right-hand operand, assigning the resulting product back to the left-hand operand.
lhs = lhs * rhs, *= is a distinct operator in Swift with its own function implementation. This design allows types to provide custom, optimized in-place mutation logic rather than relying on the compiler to automatically expand the expression.
Underlying Implementation and Memory Model
In the Swift Standard Library,*= is defined as a static method requirement within the Numeric protocol:
inout keyword on the lhs parameter dictates Swift’s “copy-in copy-out” (call by value result) memory model. The value of the left operand is conceptually copied into the function, modified by the multiplication, and then reassigned back to the original variable upon the function’s return. This reassignment mechanism is exactly why property observers, such as didSet and willSet, are triggered when using compound assignment operators on a property.
Technical Characteristics
Return Type Unlike C or Objective-C, compound assignment operators in Swift do not return the mutated value. The*= operator evaluates to Void (or ()).
Void, it cannot be chained. Attempting an expression like a *= b *= c will result in a compile-time type mismatch, as the rightmost operation evaluates to Void, which cannot be multiplied with a numeric type.
Mutability Requirement
The left-hand operand (lhs) must be a mutable variable declared with var. Attempting to use *= on a constant (let) or an immutable property will result in a compile-time error.
Type Constraints
Both operands must evaluate to the same type, and that type must conform to the Numeric protocol (e.g., Int, Double, Float, CGFloat). Swift does not perform implicit type conversion; if the operands are of different numeric types, they must be explicitly cast prior to the operation.
Overflow Behavior
The overflow behavior of the *= operator depends strictly on the underlying data type:
- Integer Types: For types like
IntorUInt8, Swift performs strict overflow checking. If the product exceeds the maximum or minimum representable bounds of thelhsdata type, the program will trap and trigger a runtime crash to prevent silent data corruption. - Floating-Point Types: For types like
Double,Float, orCGFloat, the operator does not trap on overflow. Instead, it adheres to IEEE 754 semantics, evaluating to positive or negative infinity (infor-inf) when the product exceeds the representable bounds.
Master Swift with Deep Grasping Methodology!Learn More





