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 Swift is a binary arithmetic operator used to calculate the product of two operands. At the standard library level, it is primarily defined as a static method requirement within the Numeric protocol, though the language fully supports overloading it for asymmetric type combinations.
Syntax
Numeric protocol, the operator requires a symmetric signature where both operands and the return type are identical:
Int and Double). While the Numeric protocol enforces symmetry, the Swift language itself permits asymmetric operator overloading. The standard library utilizes this capability natively for types like SIMD vectors (allowing multiplication of a vector by a scalar), and developers can define custom asymmetric overloads for their own types.
Overflow Behavior
The behavior of the * operator when a product exceeds representable bounds depends on the underlying data type:
- Integer Types: Swift performs strict overflow checking. If the computed product exceeds the bounds of the integer type, Swift traps and triggers a runtime crash to prevent silent data corruption. For modulo arithmetic and wrap-around behavior, Swift provides the dedicated overflow multiplication operator (
&*). - Floating-Point Types: Types such as
DoubleandFloatfollow the IEEE 754 standard. If a product exceeds the maximum representable finite value, the operator returns infinity (infor-inf) without trapping or crashing.
- Precedence Group:
MultiplicationPrecedence - Associativity: Left
- Evaluation Order: Evaluated before operators in the
AdditionPrecedencegroup (+,-). Multiple*operators in a single expression are evaluated from left to right.
* operator can be overloaded for custom types by defining a static func *. It can be implemented symmetrically or asymmetrically, provided the function returns a valid instance of the declared return type.
* operator serves as the foundation for the compound assignment operator *=, which multiplies the left-hand operand by the right-hand operand and assigns the result back to the left-hand operand. This requires the left-hand operand to be passed as an inout parameter (mutable via var).
Master Swift with Deep Grasping Methodology!Learn More





