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 & 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:
let result = lhs & rhs
Technical Characteristics:
  • Associativity: Left-associative.
  • Precedence: Belongs to the MultiplicationPrecedence group. Unlike C-family languages where bitwise AND has lower precedence than equality operators, Swift’s & evaluates before ComparisonPrecedence (e.g., ==) and AdditionPrecedence (e.g., |, ^). Consequently, an expression like 1 | 2 & 0 evaluates as 1 | (2 & 0), and lhs & rhs == 0 evaluates as (lhs & rhs) == 0.
  • Type Constraints: Both operands must conform to the BinaryInteger protocol and be of the exact same type.
Execution:
let lhs: UInt8 = 0b11001100 // Decimal: 204
let rhs: UInt8 = 0b10101010 // Decimal: 170

let result = lhs & rhs      // 0b10001000 (Decimal: 136)

// Precedence evaluation
let isZero = lhs & rhs == 0 // Evaluates as (lhs & rhs) == 0

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:
functionName(&variable)
Technical Characteristics:
  • In-Out Parameters: When bound to an inout parameter, & 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> or UnsafeMutablePointer<T>, & yields the memory address of the variable. Passing via & does not inherently grant mutation rights; if the parameter is UnsafePointer<T>, the memory is strictly immutable.
  • Mutability Constraints: The operand prefixed by & must be a mutable variable (declared with var), regardless of whether the receiving function mutates the memory or reads it immutably via an UnsafePointer. 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.
Execution:
// 1. Binding to an inout parameter (mutation permitted)
func increment(value: inout Int) { value += 1 }
var counter = 0
increment(value: &counter)

// 2. Binding to an UnsafePointer (strictly immutable)
func readPointer(_ ptr: UnsafePointer<Int>) { /* ... */ }
var readOnlyTarget = 42
readPointer(&readOnlyTarget) 

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:
TypeA & TypeB
Technical Characteristics:
  • 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 where clauses, and typealias definitions.
Execution:
protocol Renderable { /* ... */ }
protocol Animatable { /* ... */ }

// Typealias composition
typealias InteractiveNode = Renderable & Animatable

// Generic constraint composition using standard library protocols
func process<T: Sequence & Identifiable>(item: T) { /* ... */ }

// Existential type composition in a variable declaration
var handler: any Hashable & Sendable
Master Swift with Deep Grasping Methodology!Learn More