[Pitch] Allow conditional compilation in comma-separated syntactic lists

Introduction

Swift's conditional compilation directives currently work well around declarations and statements, but they cannot be used to conditionally include individual elements inside many comma-separated syntactic lists.

This pitch proposes allowing #if / #elseif / #else / #endif blocks at element boundaries inside selected list grammars, beginning with function and initializer parameter lists.

The goal is not to introduce C-style textual preprocessing. Conditional compilation would remain syntax-aware: each active branch would have to contribute complete syntactic elements.

Motivation

This limitation becomes noticeable in multi-target codebases that use SWIFT_ACTIVE_COMPILATION_CONDITIONS.

For example, one build target may intentionally omit an entire subsystem:

struct StoredState: Codable, Hashable {
    let selectedItemID: UUID?
    let items: [ItemInstance]

#if FULL_EDITION
    let advancedState: AdvancedStoredState?
#endif
}

Conditionally compiling the property is straightforward.

However, the corresponding initializer cannot express the same condition locally:

init(
    selectedItemID: UUID? = nil,
    items: [ItemInstance] = [],

#if FULL_EDITION
    advancedState: AdvancedStoredState? = nil,
#endif
) {
    // ...
}

Instead, the complete initializer generally has to be duplicated:

#if FULL_EDITION

init(
    selectedItemID: UUID? = nil,
    items: [ItemInstance] = [],
    advancedState: AdvancedStoredState? = nil
) {
    // common initialization
    self.advancedState = advancedState
}

#else

init(
    selectedItemID: UUID? = nil,
    items: [ItemInstance] = []
) {
    // same common initialization
}

#endif

The duplication is disproportionate to the actual difference between the two declarations.

It also introduces maintenance risk: when the shared parameters or implementation change, both conditional branches must remain synchronized.

This arises naturally in codebases supporting:

  • multiple application targets

  • reduced and full editions

  • platform-specific features

  • optional subsystems

  • compile-time feature flags

Proposed solution

Allow conditional compilation blocks where the surrounding grammar expects a sequence of complete elements.

For example:

func configure(
    mode: Mode,
#if FEATURE_X
    advancedOptions: AdvancedOptions,
#endif
    enabled: Bool
) {
}

Similarly, initializer declarations could support:

init(
    selectedItemID: UUID? = nil,
    items: [ItemInstance] = [],
#if FULL_EDITION
    advancedState: AdvancedStoredState? = nil,
#endif
) {
    // ...
}

The conditional block would operate on complete parameters rather than arbitrary tokens.

This distinction is important. The following would remain invalid:

func configure(
    value
#if FEATURE_X
    : AdvancedValue
#endif
) {
}

because the directive would split a single syntactic element.

Detailed design

Conceptually, a grammar that currently accepts:

element-list:
    element
    element-list "," element

could additionally accept conditional compilation blocks whose branches each contain zero or more complete elements.

For example:

func example(
    first: Int,
#if FEATURE_A
    second: String,
    third: Bool,
#elseif FEATURE_B
    alternative: Double,
#endif
    last: Int
) {
}

After conditional compilation, every active configuration must still result in a valid parameter list.

Conditional compilation would therefore remain structurally constrained and syntax-aware.

Initial scope

A conservative first version could support only:

  • function parameter lists

  • initializer parameter lists

These seem to provide substantial practical value while keeping the initial grammar change narrow.

If the model proves useful, similar support could later be considered for other comma-separated syntactic lists, such as:

  • argument lists

  • tuple elements

  • generic parameter lists

  • generic where requirements

  • inheritance and conformance lists

Those extensions do not need to be part of the initial proposal.

Source compatibility

This change would be purely additive.

Existing Swift source would retain its current meaning.

Code using the new syntax would require a compiler version that supports the feature.

ABI compatibility

There is no inherent ABI impact from the syntax change itself.

The resulting declarations are equivalent to declarations that can already be expressed today through larger conditional-compilation blocks.

Alternatives considered

Duplicate the complete declaration

This is the current workaround:

#if FEATURE_X

func configure(
    mode: Mode,
    advancedOptions: AdvancedOptions,
    enabled: Bool
) {
    // ...
}

#else

func configure(
    mode: Mode,
    enabled: Bool
) {
    // ...
}

#endif

While valid, it duplicates declarations and often implementation logic for a relatively small compile-time variation.

Introduce separate overloads

In some cases, overloads can avoid conditional parameter lists. However, this does not solve cases where a type or subsystem should not exist at all in a particular build configuration.

It can also unnecessarily change the public API shape.

Keep every parameter present and make it optional

Another workaround is to preserve one declaration across all builds:

init(
    selectedItemID: UUID? = nil,
    items: [ItemInstance] = [],
    advancedState: AdvancedStoredState? = nil
)

This is not always possible or desirable.

The parameter type itself may belong to a subsystem that is intentionally excluded from some targets. Keeping the parameter therefore defeats the purpose of compile-time isolation.

Use textual preprocessing

This proposal specifically does not suggest adopting a C-style textual preprocessor.

Allowing #if only around complete grammar elements preserves Swift's existing structured conditional-compilation model.

Future directions

If conditional compilation at parameter-list element boundaries proves successful, the same mechanism could potentially be generalized to other syntactic lists.

For example:

performOperation(
    value,
#if FEATURE_X
    advancedValue,
#endif
    completion: handler
)

or:

struct Container<
    Element,
#if FEATURE_X
    Metadata,
#endif
> {
}

Each context should be evaluated independently, since some list grammars may introduce parsing or type-checking complications that do not apply to parameter clauses.

Questions for discussion

  1. Is there an existing parser or grammar constraint that makes conditional compilation inside parameter clauses particularly difficult?

  2. Would restricting the initial feature to function and initializer parameters be preferable to attempting a more general solution for comma-separated lists?

  3. Should conditional compilation blocks be permitted to contain multiple elements within a branch, or should an initial implementation restrict each branch to a single element?

  4. Are there ambiguities around comma ownership or trailing commas that would require a more explicit grammar rule?

6 Likes

See SE-0330: Conditionals in Collections - #22 by tkremenek