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.

In Swift, Void is a type alias for an empty tuple, representing the absence of a specific value. Unlike C-family languages where void is a compiler keyword indicating “nothing,” Swift’s Void is a first-class type within the type system. In the Swift Standard Library, Void is explicitly defined as:
public typealias Void = ()

Type and Value Equivalence

Because Void is merely an alias, the identifier Void (the type) and () (the empty tuple type) are strictly interchangeable. Furthermore, the only possible instance (value) of the Void type is the empty tuple value ().
// Type declaration using Void
let explicitVoid: Void = ()

// Type declaration using the empty tuple type
let implicitVoid: () = ()

Function Signatures

When a function or closure does not explicitly declare a return type, the Swift compiler implicitly assigns it a return type of Void. The following three function declarations are semantically identical after type checking. However, because the Swift Abstract Syntax Tree (AST) preserves source fidelity, the syntactic differences (omitted return type vs. -> Void vs. -> ()) are represented by distinct type representation nodes in the AST:
func executeOperationA() { }
func executeOperationB() -> Void { }
func executeOperationC() -> () { }
Similarly, if a function returns Void, the compiler implicitly inserts a return () statement at the end of the function body. You can explicitly return the empty tuple value, though it is syntactically redundant:
func terminate() -> Void {
    // Implicitly returns ()
    return () 
}

Memory Layout

Because Void contains no elements, instances of Void carry no data. Consequently, the Swift compiler optimizes Void to have a size of zero bytes.
MemoryLayout<Void>.size      // 0 bytes
MemoryLayout<Void>.alignment // 1 byte
MemoryLayout<Void>.stride    // 1 byte
Note: While the size is 0, the stride and alignment are 1. Swift requires all types to have a minimum stride of 1 byte so that distinct elements in a contiguous memory block (like an Array<Void>) have unique memory addresses.

Generics Integration

Because Void is a standard type rather than a special keyword, it satisfies generic type constraints just like Int, String, or any custom struct. When a generic placeholder resolves to Void, the compiler substitutes the empty tuple type, and the generic type expects the empty tuple value ().
// Void satisfying the generic type parameter 'Success' in Result<Success, Failure>
let successState: Result<Void, Error> = .success(())

// Void satisfying a generic parameter in a custom structure
struct Container<T> {
    let payload: T
}

let emptyContainer = Container<Void>(payload: ())
Master Swift with Deep Grasping Methodology!Learn More