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.
& symbol in Swift serves three distinct syntactic and semantic roles depending on its context: as a binary bitwise AND operator for integer types, as a syntactic marker for passing references or pointers to functions, and as an infix operator for protocol composition in type signatures.
1. Bitwise AND Operator
When used as an infix operator between two integer types,& performs a bitwise AND operation. It evaluates the binary representation of both operands and returns a new integer of the same type. A bit in the resulting integer is set to 1 strictly if the corresponding bits in both the left-hand side (LHS) and right-hand side (RHS) operands are 1. Otherwise, the bit is set to 0.
Syntax:
- Associativity: Left-associative.
- Precedence: Belongs to the
MultiplicationPrecedencegroup. Unlike C-family languages where bitwise AND has lower precedence than equality operators, Swift’s&evaluates beforeComparisonPrecedence(e.g.,==) andAdditionPrecedence(e.g.,|,^). Consequently, an expression like1 | 2 & 0evaluates as1 | (2 & 0), andlhs & rhs == 0evaluates as(lhs & rhs) == 0. - Type Constraints: Both operands must conform to the
BinaryIntegerprotocol and be of the exact same type.
2. In-Out Expression Marker
When used as a prefix at a function call site,& functions strictly as a syntactic marker (parsed grammatically as an in-out-expression), not as a prefix operator. It denotes that a variable is being passed as a reference or a pointer rather than by value. Because it is a marker and not an operator, it does not participate in operator precedence, cannot be overloaded, and Swift explicitly forbids defining & as a custom prefix operator.
Syntax:
- In-Out Parameters: When bound to an
inoutparameter,&invokes Swift’s “copy-in copy-out” (call-by-value-result) memory model. The value is copied upon invocation, mutated locally, and copied back to the original memory location upon return. - Implicit Pointer Conversion: When passed to a function expecting an
UnsafePointer<T>orUnsafeMutablePointer<T>,&yields the memory address of the variable. Passing via&does not inherently grant mutation rights; if the parameter isUnsafePointer<T>, the memory is strictly immutable. - Mutability Constraints: The operand prefixed by
&must be a mutable variable (declared withvar), regardless of whether the receiving function mutates the memory or reads it immutably via anUnsafePointer. It cannot be a constant (let), a literal, or a computed property lacking a setter. - Exclusivity: Swift’s Law of Exclusivity dictates that passing a variable via
&requires exclusive access to that variable’s memory for the duration of the function call, preventing concurrent access traps.
3. Protocol Composition Operator
When used as an infix operator within a type signature,& performs protocol composition. It combines multiple protocols (and optionally up to one class) into a single requirement, representing an existential type or a generic constraint that must conform to all specified types simultaneously.
Syntax:
- Type Intersection: The operator does not create a new concrete type at runtime. Instead, it creates a compile-time intersection of type requirements.
- Constraints: The composition can include any number of protocols but is restricted to a maximum of one class type.
- Contexts: It is strictly a type-level operator used in variable declarations, function parameters, generic
whereclauses, andtypealiasdefinitions.
Master Swift with Deep Grasping Methodology!Learn More





