skip to content

A Swift AudioSettings struct changes @Clamped(0...100) var volume = 50 to @Clamped(wrappedValue: 50, 0...100) var volume: Int; why does AudioSettings(volume: 70) stop compiling?

level: seniorimportance: should knowfreq 30%

answer

  1. the memberwise initializer's parameter type
  2. initial value written with =
  3. arguments inside the attribute
  4. wrapped type versus wrapper type

basics

~20 s

The memberwise parameter takes Int only when volume has an = initial value, or no value and no attribute arguments; spelling wrappedValue: inside the attribute makes the parameter Clamped, so passing 70 no longer type-checks.

solid answer

~40 s

A wrapped property's memberwise parameter is either the wrapped type or the wrapper type. It is the wrapped type, `Int`, when the property has an `=` initial value, or has no value and no attribute arguments while the wrapper has `init(wrappedValue:)`. `@Clamped(0...100) var volume = 50` gives `init(volume: Int = 50)`. `@Clamped(wrappedValue: 50, 0...100) var volume: Int` has attribute arguments and no `=` value, so the parameter becomes `volume: Clamped` with that call as its default, and `AudioSettings(volume: 70)` fails with "cannot convert value of type 'Int' to expected argument type 'Clamped'". I'd restore the `=` form or write an explicit `init(volume: Int = 50)` that assigns `_volume`.

code

swift · 29 lines
swift
@propertyWrapper
struct Clamped {
    private var value: Int
    private let range: ClosedRange<Int>

    init(wrappedValue: Int, _ range: ClosedRange<Int>) {
        self.range = range
        value = min(max(wrappedValue, range.lowerBound), range.upperBound)
    }

    var wrappedValue: Int {
        get { value }
        set { value = min(max(newValue, range.lowerBound), range.upperBound) }
    }
}

struct AudioSettings {
    @Clamped(0...100) var volume = 50
    // memberwise: init(volume: Int = 50)
}

struct AudioSettingsV2 {
    @Clamped(wrappedValue: 50, 0...100) var volume: Int
    // memberwise: init(volume: Clamped = Clamped(wrappedValue: 50, 0...100))
}

let before = AudioSettings(volume: 70)
let after = AudioSettingsV2(volume: Clamped(wrappedValue: 70, 0...100))
print(before.volume, after.volume)   // Prints "70 70"

go deeper

for a junior

Recall that = 50 on a wrapped property is passed to the wrapper's init(wrappedValue:), and that the struct's memberwise initializer still has a volume parameter.

for a middle

Explain when the memberwise parameter is Int versus Clamped and which wrapper initializer each declaration form calls.

for a senior

Diagnose the call-site break from the declaration diff alone, name the exact diagnostic, and pick the fix that keeps the wrapper out of the struct's API.

for a principal

Treat the spelling of wrapped declarations in public types as API surface, and decide whether such types should declare explicit initializers instead of relying on memberwise synthesis.

## The rule behind the break A struct's **memberwise initializer** gets one parameter per stored property. A wrapped property is really computed; the stored thing is the private wrapper instance `_volume`. So the compiler has to decide whether the memberwise parameter for `volume` takes the **wrapped type** (`Int`) and builds the wrapper itself, or takes the **wrapper type** (`Clamped`) and stores it as given. It takes the wrapped type when: 1. the property has an **`=` initial value**, as in `@Clamped(0...100) var volume = 50`; the parameter becomes `volume: Int = 50` and the initializer calls `Clamped(wrappedValue: volume, 0...100)`; or 2. the property has **no initial value, no arguments in the attribute**, and the wrapper supports `init(wrappedValue:)`, as in `@Trimmed var email: String`. In every other case the parameter takes the **wrapper type**, with the attribute's initializer call as its default argument. `@Clamped(wrappedValue: 50, 0...100) var volume: Int` has attribute arguments and no `=` value, so the memberwise initializer becomes `init(volume: Clamped = Clamped(wrappedValue: 50, 0...100))`. `AudioSettings(volume: 70)` now passes an `Int` where a `Clamped` is expected: ```plaintext Settings.swift:30:38: error: cannot convert value of type 'Int' to expected argument type 'Clamped' ``` Nothing about `Clamped` changed. Only the spelling of the declaration did, and that spelling is part of the struct's initializer signature. ## The four declaration forms side by side These results come from compiling each form with the Swift 6.4 compiler: | Declaration | Wrapper initializer at declaration | Memberwise parameter | |---|---|---| | `@Trimmed var email: String` | none until the initializer runs | `email: String` | | `@Clamped(0...100) var volume = 50` | `Clamped(wrappedValue: 50, 0...100)` | `volume: Int = 50` | | `@Clamped(wrappedValue: 50, 0...100) var volume: Int` | the same call, spelled out | `volume: Clamped = Clamped(wrappedValue: 50, 0...100)` | | `@Clamped(0...100) var volume: Int` | none possible | error: `missing argument for parameter 'wrappedValue' in call` | The last row matters too: attribute arguments without a `wrappedValue` need an initializer that accepts exactly those arguments. When `Clamped` only has `init(wrappedValue:_:)`, there is no way to build `_volume`, and the declaration itself fails to compile. ## How Swift picks the wrapper's initializer - **No value, no arguments** uses `init()` if the wrapper has one; otherwise the type's initializer must initialize the property, either by assigning it (which calls `init(wrappedValue:)`) or by assigning `_name` directly. - **`= value`** is sugar for a `wrappedValue:` argument and is combined with any attribute arguments: `@Clamped(0...100) var volume = 50` calls `Clamped(wrappedValue: 50, 0...100)`. - **Arguments in the attribute** are passed to the initializer whose labels match. Writing `wrappedValue:` there yourself is allowed, but it moves the value out of the `=` form that the memberwise rule keys on. ## Fixing the break There are three honest fixes, in rough order of preference: 1. Go back to `@Clamped(0...100) var volume = 50`, which keeps `AudioSettings(volume: 70)` and `AudioSettings()` working. 2. Write the initializer yourself and build the wrapper in it: ```swift struct AudioSettingsV3 { @Clamped(wrappedValue: 50, 0...100) var volume: Int init(volume: Int = 50) { _volume = Clamped(wrappedValue: volume, 0...100) } } ``` 3. Accept the wrapper-typed parameter and update call sites to pass `Clamped(wrappedValue: 70, 0...100)`; this exposes the wrapper in the struct's API, which is rarely what you want. Inside a hand-written initializer, assigning `_volume` builds a fresh wrapper with the range you choose, replacing the one the declaration built. ## Assigning a wrapped property in your own initializer Whether `volume = x` in an initializer creates the wrapper or updates it depends on whether `_volume` already holds one: - If the declaration gives no initial value and no attribute arguments, as with `@Trimmed var email: String`, the **first** `email = raw` in the initializer is treated as `_email = Trimmed(wrappedValue: raw)`. Later assignments call the setter. - If the declaration already builds the wrapper, through `= 50` or arguments in the attribute, every assignment calls the setter of that existing wrapper. Swift's definite-initialization checks apply to the backing storage in both cases, so reading `email` before the first assignment is a compile error, as it would be for any stored property. ## Why it bites in review - The diff looks cosmetic: same wrapper, same range, same default value. - The break shows up at call sites, often in another file or module, not at the declaration. - Tests that use the zero-argument `AudioSettings()` still pass, because the wrapper-typed parameter has a default. - The general rules of memberwise initializers (when they exist, their access level) are unchanged; only the parameter type for the wrapped property moved.

  • Why does AudioSettingsV2() with no arguments still compile after the change?
    The wrapper-typed memberwise parameter gets the attribute's initializer call as its default argument, `Clamped(wrappedValue: 50, 0...100)`. A zero-argument call uses that default, so tests built on `AudioSettingsV2()` keep passing while every caller that passes a volume breaks.
  • What happens with @Clamped(0...100) var volume: Int when Clamped has only init(wrappedValue:_:)?
    It does not compile: Swift tries to build `_volume` from the attribute arguments alone and reports `missing argument for parameter 'wrappedValue' in call`. Either add `= 50`, spell `wrappedValue:` in the attribute, or give the wrapper an initializer that takes just the range.

  • The memberwise parameter for a wrapped property is always the wrapped type.
  • Writing wrappedValue: inside the attribute is identical to writing = 50.
  • The break means Clamped lost its init(wrappedValue:) initializer.
  • A wrapped property never appears in the memberwise initializer at all.