Skip to main content

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.

The * 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
let product = lhs * rhs
Protocol Definition and Type Rules For types conforming to the Numeric protocol, the operator requires a symmetric signature where both operands and the return type are identical:
static func * (lhs: Self, rhs: Self) -> Self
Swift does not perform implicit type coercion between different numeric types (e.g., 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 Double and Float follow the IEEE 754 standard. If a product exceeds the maximum representable finite value, the operator returns infinity (inf or -inf) without trapping or crashing.
Precedence and Associativity
  • Precedence Group: MultiplicationPrecedence
  • Associativity: Left
  • Evaluation Order: Evaluated before operators in the AdditionPrecedence group (+, -). Multiple * operators in a single expression are evaluated from left to right.
Operator Overloading The * 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.
struct CustomType {
    var value: Int
    
    // Symmetric overloading
    static func * (lhs: CustomType, rhs: CustomType) -> CustomType {
        return CustomType(value: lhs.value * rhs.value)
    }

    // Asymmetric overloading
    static func * (lhs: CustomType, rhs: Int) -> CustomType {
        return CustomType(value: lhs.value * rhs)
    }
}
Compound Assignment The * 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).
static func *= (lhs: inout Self, rhs: Self)
Master Swift with Deep Grasping Methodology!Learn More