# 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:** 1

<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 19, 2019, 8:59pm UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/1 "2019-06-19T20:59:26Z")

</div>

Hi Team,

I wanted to bring up the current magical behavior of Optional auto initialization to nil when using `?`

Currently in swift you can declare an Optional `var`iable in this way

```swift
var foo: String?
print(foo) // prints nil

```

swift will automatically assign `.none` to the value. The above is somewhat equivalent to the below code.

```swift
var foo: String? = nil // .none
print(foo) // prints nil

```

but this behavior is only available when using `?`. The below does not work.

```swift
var someValue11:Optional<String> 
// print(someValue11) // error: variable 'someValue11' used before being initialized 

```

> [@\[Pitch\] Optional parameters in functions should default to \`nil\`, allow omission](https://forums.swift.org/t/pitch-optional-parameters-in-functions-should-default-to-nil-allow-omission/25788/39):
>
> I’d love to see someone pitch and drive a proposal to remove that behavior, by the way. Optionals are the only type in Swift that can be default initialized in this manner, and it feels like an unnecessary bit of magic to me.

I think this behavior is confusing specially when dealing with implicit unwrapped optional.

```swift
var someValue33:String!
_ = someValue33 + "?") // Boom Crash. No different from an uninitialized variable error

```

> <https://github.com/apple/swift/issues/53322>
>
> | | |
> |------------------|-----------------|…
> |Previous ID | SR-10931 |
> |Radar | None |
> |Original Reporter | @masters3d |
> |Type | Bug |
> 
> \<details\>
> \<summary\>Environment\</summary\>
> 
> swift 5.0
> 
> 
> \</details\>
> 
> \<details\>
> \<summary\>Additional Detail from JIRA\</summary\>
> 
> | | |
> |------------------|-----------------|
> |Votes | 2 |
> |Component/s | Compiler |
> |Labels | Bug |
> |Assignee | None |
> |Priority | Medium |
> 
>    
> 
> md5: 0e6cd4419cd22cf69c97288a90ec7e94
> 
> \</details\>
> 
> 
> 
> 
> \*\*relates to\*\*:  
> \* \[SR-354\](https://bugs.swift.org/browse/SR-354) Non-optional variable tuple of optional values should not have default value  
> \* \[SR-801\](https://bugs.swift.org/browse/SR-801) TestPOUnwrapping.py - PO for optional class members set to nil are showing up as if 0 or ""  
> \* \[SR-11768\](https://bugs.swift.org/browse/SR-11768) Stored property declaration + initialization expressions cannot be inlinable  
> \* \[SR-11777\](https://bugs.swift.org/browse/SR-11777) 'var something: 'Type?' always get double initialised  
> 
> 
> \*\*Issue Description:\*\*
> 
> 
> I am having a hard time understanding why T? variables auto initialize to nil when Optional\\\<T\\\> variables do not. This seems like a bug to me. If its not a bug, where else is this feature being used? 
> 
> \`\`\` java
> do{ 
> let someValue1:Optional\<String\> 
> //print(someValue1) // error: constant 'someValue1' used before being initialized 
> var someValue11:Optional\<String\> 
> // print(someValue11) // error: variable 'someValue11' used before being initialized 
> let someValue2:String? 
> // print(someValue2) //error: constant 'someValue2' used before being initialized 
> var someValue22:String? 
> print(someValue22) // Compiler happy. Why? What is the technical reason why this should work. 
> 
> // Now consider the following type of optional. 
> var someValue33:String!
> print(someValue33 + "") // Boom Crash. Implicit initialization here is actually dangerous. 
> }
> \`\`\`

I propose that we deprecate this to make swift more consistent.

```swift
var foo: String?
// print(foo) // error: variable 'foo' used before being initialized. <Insert fixit here to add = nil>

```

What does the community think?

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [June 19, 2019, 9:01pm UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/2 "2019-06-19T21:01:57Z")

</div>

I don't see how the calculus has changed. It's a trivial rule to learn and saves more than it hurts.

---

<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 19, 2019, 9:21pm UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/3 "2019-06-19T21:21:41Z")

</div>

It's also confusing because it applies to local variables but not to properties.

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [June 19, 2019, 9:23pm UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/4 "2019-06-19T21:23:02Z")

</div>

It applies to `var` but not `let`, both for local variables and properties.

---

<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 19, 2019, 9:27pm UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/5 "2019-06-19T21:27:45Z")

</div>

Well the fact that I got that wrong just shows how confusing it is! 🙂

---

<div class="post-metadata">

**Author:** ![blangmuir](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/blangmuir/32/277_2.png) [@blangmuir](https://forums.swift.org/u/blangmuir)\
**Post date:** [June 19, 2019, 11:45pm UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/6 "2019-06-19T23:45:12Z")

</div>

I'd be very happy to see the implicit initialization removed. In addition to the issues mentioned in your pre-pitch, I want

```swift
  var x: Int?
  if b {
    x = 1
  } else {
  }
  use(x)

```

to tell me that I haven't initialized `x` on all paths like it does for any other type.

---

<div class="post-metadata">

**Author:** ![harlanhaskins](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/harlanhaskins/32/333_2.png) [@harlanhaskins](https://forums.swift.org/u/harlanhaskins)\
**Post date:** [June 19, 2019, 11:49pm UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/7 "2019-06-19T23:49:08Z")

</div>

Undocumented extension of this feature: tuples of optional types are default initialized as well.

```swift
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:** ![Alejandro](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alejandro/32/46197_2.png) [@Alejandro](https://forums.swift.org/u/Alejandro)\
**Post date:** [June 19, 2019, 11:52pm UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/8 "2019-06-19T23:52:24Z")

</div>

This would clean up a lot of implicit rules and edge cases in the compiler! 😄

---

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [June 19, 2019, 11:54pm UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/9 "2019-06-19T23:54:41Z")

</div>

> [@Jon\_Shier](#):
>
> I don't see how the calculus has changed. It's a trivial rule to learn and saves more than it hurts.

i know what the rule is but at this point i’ve taken to just always writing out the `nil`s to avoid confusion with `let` deferred initialization. will attest that i too find this pretty confusing.

---

<div class="post-metadata">

**Author:** ![jarod](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jarod/32/364_2.png) [@jarod](https://forums.swift.org/u/jarod)\
**Post date:** [June 19, 2019, 11:55pm UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/10 "2019-06-19T23:55:05Z")

</div>

I've run into problems a handful of times where I forgot to set an optional property during initialization because it defaulted to nil. I'd be happy to see this rule removed mainly for that reason, but also because it's a special case rule that doesn't pull its weight in terms of convenience imo.

---

<div class="post-metadata">

**Author:** ![bobergj](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@bobergj](https://forums.swift.org/u/bobergj)\
**Post date:** [June 20, 2019, 12:13am UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/11 "2019-06-20T00:13:28Z")

</div>

> [@jarod](#):
>
> I've run into problems a handful of times where I forgot to set an optional property during initialization because it defaulted to nil.

Then make that property non-optional? Perhaps you are tracking some state where, instead of an optional, it would be better to use your own enum with cases named after the states + associated values for the data.

Since most people above seem positive to this pitch, just want to add a -1.

---

<div class="post-metadata">

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

</div>

I agree that a custom enum is sometimes more appropriate than an optional, but not always. In cases where an optional is more appropriate, I wouldn't want to create an enum just to protect against this issue.

---

<div class="post-metadata">

**Author:** ![Nobody1707](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nobody1707/32/2169_2.png) [@Nobody1707](https://forums.swift.org/u/Nobody1707)\
**Post date:** [June 20, 2019, 1:15am UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/13 "2019-06-20T01:15:32Z")

</div>

I'm +1 on this. I don't feel it pulls it's weight, and I agree with @taylorswift about the `let` deferred initialization issue.

---

<div class="post-metadata">

**Author:** ![ladislas](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ladislas/32/1437_2.png) [@ladislas](https://forums.swift.org/u/ladislas)\
**Post date:** [June 20, 2019, 8:50am UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/14 "2019-06-20T08:50:39Z")

</div>

+1 as well for the reasons above.

---

<div class="post-metadata">

**Author:** ![BenHetherington](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/benhetherington/32/9480_2.png) [@BenHetherington](https://forums.swift.org/u/BenHetherington)\
**Post date:** [June 20, 2019, 10:19am UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/15 "2019-06-20T10:19:08Z")

</div>

I don't normally post here, but I also wanted to balance things out a bit with a -1.

Personally, I find it convenient to declare and initialise optionals with `nil` in one go (sort of like the opposite to type inference for non-optional variables, i.e. writing out the type instead of the value). Despite the anecdotes above, the rule also seems straightforward.

_(Equally, I'm one of the weirdos that liked doing `var x = Int?()` in the Swift 2 days, so make of that what you will!)_

---

<div class="post-metadata">

**Author:** ![kyzmitch](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kyzmitch/32/5377_2.png) [@kyzmitch](https://forums.swift.org/u/kyzmitch)\
**Post date:** [June 20, 2019, 10:56am UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/16 "2019-06-20T10:56:28Z")

</div>

to protect yourself from missed initialisation in that case when you have `if else` I use `let`

---

<div class="post-metadata">

**Author:** ![kyzmitch](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kyzmitch/32/5377_2.png) [@kyzmitch](https://forums.swift.org/u/kyzmitch)\
**Post date:** [June 20, 2019, 10:58am UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/17 "2019-06-20T10:58:27Z")

</div>

it is like `init` with default parameters but for Optional, isn't it?

---

<div class="post-metadata">

**Author:** ![anandabits](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anandabits/32/355_2.png) [@anandabits](https://forums.swift.org/u/anandabits)\
**Post date:** [June 20, 2019, 1:23pm UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/18 "2019-06-20T13:23:43Z")

</div>

+1. There is no reason for a special case implicit initialization.

---

<div class="post-metadata">

**Author:** ![spadafiva](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/spadafiva/32/6635_2.png) [@spadafiva](https://forums.swift.org/u/spadafiva)\
**Post date:** [June 20, 2019, 1:42pm UTC](https://forums.swift.org/t/prepitch-optional-variables-should-require-explicit-initialization/26077/19 "2019-06-20T13:42:22Z")

</div>

+1

---

<div class="post-metadata">

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

</div>

With this change, would all `@IBOutlet`s have to be explicitly initialized to nil then?

`@IBOutlet var label: UILabel! = nil`

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