# Default auto generated init(): member property with default value must be 'var', cannot be 'let'?

**URL:** <https://forums.swift.org/t/default-auto-generated-init-member-property-with-default-value-must-be-var-cannot-be-let/31665>\
**Category:** Using Swift\
**Created:** [December 13, 2019, 11:43pm UTC](https://forums.swift.org/t/default-auto-generated-init-member-property-with-default-value-must-be-var-cannot-be-let/31665 "2019-12-13T23:43:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [December 13, 2019, 11:43pm UTC](https://forums.swift.org/t/default-auto-generated-init-member-property-with-default-value-must-be-var-cannot-be-let/31665/1 "2019-12-13T23:43:07Z")

</div>

This is what I want: member property to be a `let`, with default value that can be overridden **and** make use of compiler auto generated `init()`.

```swift
// This is how I want write out: `x` is `let`, with default overridable value
struct Foo {
    let x: Int? = nil
}

let foo = Foo(x: 123) // **error:**  **argument passed to call that takes no arguments**

```

So I must write out the `init`:

```swift
Struct Foo {
    let x: Int?

    init(x: Int? = nil) {
        self.x = x
    }
}

```

Or change `x` to a `var`:

```swift
struct Foo {
    var x: Int? = nil
}

// So now this work, but x is a `var` :(
let foo = Foo(x: 123)

```

Is there anyway to get what I want?

---

<div class="post-metadata">

**Author:** ![jonprescott](https://avatars.discourse-cdn.com/v4/letter/j/e36b37/32.png) [@jonprescott](https://forums.swift.org/u/jonprescott)\
**Post date:** [December 14, 2019, 1:38am UTC](https://forums.swift.org/t/default-auto-generated-init-member-property-with-default-value-must-be-var-cannot-be-let/31665/2 "2019-12-14T01:38:35Z")

</div>

Why do you want to do what you've stated? As I read the language spec, it kind of goes against what a `let` (a constant value that can't be changed once initialized) is. If you want a constant value that gets initialized at run-time with a value, then you have to specify the initializer (your second case). In the first case, you initialized `x` with a nil value, which sets `x`'s value for the remainder of the execution, if I read the language reference correctly. In your first example, you initialized `x` in the declaration, thus, the auto-generated init has nothing to do, and, since you only have one instance variable, it's a constant, and it's already initialized, the initializer doesn't have any arguments.

---

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [December 14, 2019, 2:50am UTC](https://forums.swift.org/t/default-auto-generated-init-member-property-with-default-value-must-be-var-cannot-be-let/31665/3 "2019-12-14T02:50:45Z")

</div>

I just want to achieve this:

```swift
Struct Foo {
    let x: Int?

    init(x: Int? = nil) {
        self.x = x
    }
}

```

without having to manually write the `init()`, just able to use the compiler generate member wise `init()`.

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [December 14, 2019, 2:57am UTC](https://forums.swift.org/t/default-auto-generated-init-member-property-with-default-value-must-be-var-cannot-be-let/31665/4 "2019-12-14T02:57:17Z")

</div>

There's none. That's as concise as you can get.

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [December 14, 2019, 3:07am UTC](https://forums.swift.org/t/default-auto-generated-init-member-property-with-default-value-must-be-var-cannot-be-let/31665/5 "2019-12-14T03:07:36Z")

</div>

Why do you want to declare the property with `let`?

In the vast majority of situations, the right design is to declare all properties `var`. If you want to prevent mutation, you can use `private(set)`.

---

<div class="post-metadata">

**Author:** ![jonprescott](https://avatars.discourse-cdn.com/v4/letter/j/e36b37/32.png) [@jonprescott](https://forums.swift.org/u/jonprescott)\
**Post date:** [December 14, 2019, 3:23am UTC](https://forums.swift.org/t/default-auto-generated-init-member-property-with-default-value-must-be-var-cannot-be-let/31665/6 "2019-12-14T03:23:29Z")

</div>

private(set) still allows you to mutate the value, just greatly constrains where it can get set, namely, within the object that owns the property. If you want a constant that can be set at runtime, and can't be mutated at all, this is a pretty good approach. But, you have to write the initializer.

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [December 14, 2019, 3:30am UTC](https://forums.swift.org/t/default-auto-generated-init-member-property-with-default-value-must-be-var-cannot-be-let/31665/7 "2019-12-14T03:30:59Z")

</div>

> [@jonprescott](#):
>
> If you want a constant that can be set at runtime, and can't be mutated at all, this is a pretty good approach.

I repeat my question: why does @young want this?

I posit, again, that it is almost always _not_ what one actually wants, especially for a value-type.

---

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [December 14, 2019, 5:06am UTC](https://forums.swift.org/t/default-auto-generated-init-member-property-with-default-value-must-be-var-cannot-be-let/31665/8 "2019-12-14T05:06:06Z")

</div>

It's for some SwiftUI widget like this:

```swift
struct Foo: View {
    @Binding var value1: Double
    @Binding var value2: Double
    // etc, etc
    let onTouch: (() -> ())? // these should have default nil value
    let onLongPress: (() -> ())?
    let iconFactory: ((Double) -> Image)?
    // etc, etc
}

```

I want the optional callback closures to be `let` and default nil, at the same time, I want to avoid having to write the initializer...it's kind of tedious and anytime I add or subtract something, I must revise the hand written initializer. Seem like a perfect task for the compiler 🙂

(should compiler generated `init()` support `let` property with default value?)

---

<div class="post-metadata">

**Author:** ![young](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/young/32/7878_2.png) [@young](https://forums.swift.org/u/young)\
**Post date:** [March 9, 2020, 7:12pm UTC](https://forums.swift.org/t/default-auto-generated-init-member-property-with-default-value-must-be-var-cannot-be-let/31665/9 "2020-03-09T19:12:59Z")

</div>

There is a proposal to fix this problem with @initializable: [Explicit Memberwise Initializers](https://forums.swift.org/t/explicit-memberwise-initializers/22893)
