# PrePitch: Optional variables should require explicit initialization

**URL:** <https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077>\
**Category:** Pitches\
**Created:** [June 19, 2019, 8:59pm UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077 "2019-06-19T20:59:26Z")\
**Posts on this page:** 20\
**Page:** 4

<div class="post-metadata">

**Author:** ![Optional](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/optional/32/12543_2.png) [@Optional](https://forums.swift.org/u/Optional)\
**Post date:** [June 23, 2019, 5:00am UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/62 "2019-06-23T05:00:45Z")

</div>

-111111111111111

---

<div class="post-metadata">

**Author:** ![masters3d](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@masters3d](https://forums.swift.org/u/masters3d)\
**Post date:** [June 23, 2019, 5:01am UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/63 "2019-06-23T05:01:06Z")

</div>

I think at least some form of this proposal should be put up for review specially for the tuple case which is just bizarre. I will start another thread about the @Default. Thanks for the question!

> [@PrePitch: Optional variables should require explicit initialization](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/7):
>
> Undocumented extension of this feature: tuples of optional types are default initialized as well. var x: (Int?, ((Bool?, Int?), Int?)) print(x) // prints (nil, ((nil, nil), nil)) If we remove this behavior, we should document that the tuple behavior is removed as well.

---

<div class="post-metadata">

**Author:** ![clayellis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/clayellis/32/43646_2.png) [@clayellis](https://forums.swift.org/u/clayellis)\
**Post date:** [June 23, 2019, 5:03am UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/64 "2019-06-23T05:03:34Z")

</div>

Good to know. And yeah, I'd agree, that case is pretty odd. I'd be curious to know if anyone is actually using that as a "feature"

---

<div class="post-metadata">

**Author:** ![masters3d](https://avatars.discourse-cdn.com/v4/letter/m/c4cdca/32.png) [@masters3d](https://forums.swift.org/u/masters3d)\
**Post date:** [June 23, 2019, 5:04am UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/65 "2019-06-23T05:04:31Z")

</div>

No sure but its an old bug report. [[SR-354] Non-optional variable tuple of optional values should not have default value · Issue #42974 · apple/swift · GitHub](https://bugs.swift.org/browse/SR-354)

---

<div class="post-metadata">

**Author:** ![fatto](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/fatto/32/6159_2.png) [@fatto](https://forums.swift.org/u/fatto)\
**Post date:** [June 23, 2019, 9:07am UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/66 "2019-06-23T09:07:02Z")

</div>

Definetly -1, I dont like the idea to add useless code to make explicit an intuitive default

---

<div class="post-metadata">

**Author:** ![sveinhal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sveinhal/32/3992_2.png) [@sveinhal](https://forums.swift.org/u/sveinhal)\
**Post date:** [June 23, 2019, 11:06am UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/67 "2019-06-23T11:06:47Z")

</div>

> [@fatto](#):
>
> I dont like the idea to add useless code

It’s ok to both like and prefer the current implementation, but to call it useless is factually incorrect. Explicit initialization can prevent real bugs, and there is a real semantic difference between uninitialized and nil. There are real, measurable use cases.

Please refrain from stating opinions as fact.

What you mean to say, is that you don’t care much about that difference and that the convenience of implicit initialization far outweighs the costs. Which is a fine position to have. But it doesn’t make the suggested change “useless”.

---

<div class="post-metadata">

**Author:** ![dwaite](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dwaite/32/1200_2.png) [@dwaite](https://forums.swift.org/u/dwaite)\
**Post date:** [June 23, 2019, 4:11pm UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/68 "2019-06-23T16:11:48Z")

</div>

To be honest, I don't have a single opinion here. If you were to default initialize a value, `nil` is the most appropriate concept to use. I could even see a case for this to be expanded to encompass all types supporting `ExpressibleByNilLiteral`, rather than being tied to the `?` and `!` shortcuts for `Optional`s.

I do think a rule change around properties needs to take into account lazy properties and property wrappers, both of which also change the perceived behavior of initialization.

---

<div class="post-metadata">

**Author:** ![David\_Catmull](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_catmull/32/1007_2.png) [@David\_Catmull](https://forums.swift.org/u/David_Catmull)\
**Post date:** [June 24, 2019, 3:20pm UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/69 "2019-06-24T15:20:37Z")

</div>

> [@fatto](#):
>
> Definetly -1, I dont like the idea to add useless code to make explicit an intuitive default

Sure, the default is intuitive, but it's inconsistent. Why should sugared optionals be implicitly initialized when nothing else is? Not even `Optional<T>` which is supposed to be the same thing.

---

<div class="post-metadata">

**Author:** ![nicklockwood](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nicklockwood/32/3056_2.png) [@nicklockwood](https://forums.swift.org/u/nicklockwood)\
**Post date:** [June 24, 2019, 8:01pm UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/70 "2019-06-24T20:01:19Z")

</div>

I can see a case for requiring explicit `nil` for (?) Optionals. For (!) IUOs it would seem very counter intuitive to have to explicitly set them to `nil` at init time since their semantic purpose is _deferred_ initialization.

---

<div class="post-metadata">

**Author:** ![clayellis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/clayellis/32/43646_2.png) [@clayellis](https://forums.swift.org/u/clayellis)\
**Post date:** [June 25, 2019, 4:56am UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/71 "2019-06-25T04:56:18Z")

</div>

> [@David\_Catmull](#):
>
> Why should sugared optionals be implicitly initialized when nothing else is?

For reasons laid out higher up in the discussion.

---

<div class="post-metadata">

**Author:** ![kaiengelhardt](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kaiengelhardt/32/406_2.png) [@kaiengelhardt](https://forums.swift.org/u/kaiengelhardt)\
**Post date:** [June 25, 2019, 4:45pm UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/72 "2019-06-25T16:45:44Z")

</div>

I’m -1 on this. In my opinion implicitly initializing optionals to nil is intuitive for experienced programmers and beginners.

A lot of my code as well as other code I have seen relies on this feature and removing it would cause a lot of code churn.

---

<div class="post-metadata">

**Author:** ![cedric](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cedric/32/1331_2.png) [@cedric](https://forums.swift.org/u/cedric)\
**Post date:** [June 26, 2019, 1:33am UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/73 "2019-06-26T01:33:12Z")

</div>

Definitely -1 on this. I’d be more inclined to say optionals shouldn’t be able to be made through Optional.

Less boiler plate code, not more.

---

<div class="post-metadata">

**Author:** ![idrougge](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/idrougge/32/3885_2.png) [@idrougge](https://forums.swift.org/u/idrougge)\
**Post date:** [June 30, 2019, 12:09am UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/74 "2019-06-30T00:09:46Z")

</div>

I have spent quite a lot of time (or at least a lot of commits) removing explicit assignments with nil for optional members the last few months on an inherited code base. I don't particularly fancy the thought of putting all those assignments back, since they do nothing for readability — if anything, they add noise.

---

<div class="post-metadata">

**Author:** ![Doug](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/doug/32/7679_2.png) [@Doug](https://forums.swift.org/u/Doug)\
**Post date:** [July 1, 2019, 7:16am UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/75 "2019-07-01T07:16:36Z")

</div>

Definitely a -1 on this from me, purely from a code-breaking perspective.

I can see this affecting code far and wide, including sources outside of maintained codebases such as blog posts, stackoverflow and tutorial videos (which are likely hard to update). Whilst I see the benefit of it technically being more correct, I don’t believe this outweighs the potential chaos it will cause.

---

<div class="post-metadata">

**Author:** ![idrougge](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/idrougge/32/3885_2.png) [@idrougge](https://forums.swift.org/u/idrougge)\
**Post date:** [July 1, 2019, 3:34pm UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/76 "2019-07-01T15:34:32Z")

</div>

There even is a Swiftlint rule discouraging explicit initialisation with `nil`: [SwiftLint/Rules.md at main · realm/SwiftLint · GitHub](https://github.com/realm/SwiftLint/blob/master/Rules.md#redundant-optional-initialization)

---

<div class="post-metadata">

**Author:** ![StefanCosminR](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/stefancosminr/32/33263_2.png) [@StefanCosminR](https://forums.swift.org/u/StefanCosminR)\
**Post date:** [July 2, 2019, 11:07am UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/77 "2019-07-02T11:07:25Z")

</div>

-1  
When I set a property as optional I expect it to be nil by default and the code is ready to deal with having nil as a value for that property. I think a linter should provide warnings if it's not explicitly set to nil for those who prefer to have this, but the convenience outweighs the "magic" for me.

---

<div class="post-metadata">

**Author:** ![sveinhal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sveinhal/32/3992_2.png) [@sveinhal](https://forums.swift.org/u/sveinhal)\
**Post date:** [July 2, 2019, 3:22pm UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/78 "2019-07-02T15:22:13Z")

</div>

> [@Doug](#):
>
> Definitely a -1 on this from me, purely from a code-breaking perspective.
> 
> I can see this affecting code far and wide, including sources outside of maintained codebases such as blog posts, stackoverflow and tutorial videos (which are likely hard to update). Whilst I see the benefit of it technically being more correct, I don’t believe this outweighs the potential chaos it will cause.

This could be solved by deprecating rather than removing the current behaviour, and issue warnings at call site, warning the programmer that the implicit initialisation of `nil` may be unwanted, but still allow it. This behaviour could also be guarded through some sort of environment variable it would be an error in new (or converted) xcode projects, but a warning in older code bases.

---

<div class="post-metadata">

**Author:** ![idrougge](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/idrougge/32/3885_2.png) [@idrougge](https://forums.swift.org/u/idrougge)\
**Post date:** [July 4, 2019, 8:54am UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/79 "2019-07-04T08:54:50Z")

</div>

Consider this code:

```swift
struct Thing: Decodable {
    let field: Int
    let optional: Int?
}

        let json = """
        {
        "field": 123,
        "optional": 999
        }
        """.data(using: .utf8)!
        let thing = try! JSONDecoder().decode(Thing.self, from: json)

```

> **Thing(field: 123, optional: Optional(999))**

Now, change the declaration of `optional` so that you don't get a warning from the proposed change:

```swift
    let optional: Int? = nil

```

Now you get:

> **Thing(field: 123, optional: nil)**

---

<div class="post-metadata">

**Author:** ![sveinhal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sveinhal/32/3992_2.png) [@sveinhal](https://forums.swift.org/u/sveinhal)\
**Post date:** [July 4, 2019, 9:35am UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/80 "2019-07-04T09:35:32Z")

</div>

> [@idrougge](#):
>
> Now, change the declaration of `optional` so that you don't get a warning from the proposed change:
> 
> ```swift
> let optional: Int? = nil
> 
> ```

No one has suggested this.  
Firstly, `let` already works correctly. Secondly, if it was declared as

```swift
var optional: Int?

```

You still wouldn't get a warning on this line. However, you would get a warning in your initialisers that you "Return from initializer without initializing all stored properties"

Consider this example:

```swift
struct Thing: Decodable {
    let field: Int
    let optional: Int?

    init(field: Int, optional: Int?) {
        self.field = field
    } // error on this line
}

```

Then replace the `let`s with `var`s and observe that the error goes away. This proposal suggests that they behave the same.

---

<div class="post-metadata">

**Author:** ![idrougge](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/idrougge/32/3885_2.png) [@idrougge](https://forums.swift.org/u/idrougge)\
**Post date:** [July 4, 2019, 11:39am UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/81 "2019-07-04T11:39:59Z")

</div>

How do you mean that "`let` already works correctly"?

This proposal would add `init` boilerplate for no particular gain.

[Previous page](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077.md?page=3)

[Next page](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077.md?page=5)
