# \`if let\` shorthand

**URL:** <https://forums.swift.org/t/if-let-shorthand/54230>\
**Category:** Pitches\
**Created:** [December 22, 2021, 10:23pm UTC](https://forums.swift.org/t/if-let-shorthand/54230 "2021-12-22T22:23:36Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![christianselig](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/christianselig/32/9809_2.png) [@christianselig](https://forums.swift.org/u/christianselig)\
**Post date:** [December 23, 2021, 2:04pm UTC](https://forums.swift.org/t/if-let-shorthand/54230/21 "2021-12-23T14:04:04Z")

</div>

Please! This would be such an awesome ergonomic improvement. Xcode 13 adding autocomplete to `if let foo = foo` was a big improvement, but would still be so much better to not have the extra cruft. (And the fact that automatic tooling was added to compensate for something everyone types out regularly seems like a clear vote for this syntax in and of itself)

This feels like one of the last big head-scratching pain points of using Swift that virtually every developer encounters and would be a big win to implement.

I agree that the `let` seems important in declaration due to the potential conditional nature of an if statement that doesn't apply to `for` loops. I'm also not a big fan of a custom `unwrap` keyword, being able to unwrap an optional and check another condition in the same line is very nice in Swift currently and it'd be a shame to lose that.

I feel like `if let foo` is something every Swift developer has came up with in their head at some point (it feels like a monthly discussion on Twitter at times) and seems like an obvious/clear win.

---

<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 23, 2021, 2:11pm UTC](https://forums.swift.org/t/if-let-shorthand/54230/22 "2021-12-23T14:11:25Z")

</div>

It'd be nice if you also summarize what's been discussed so far (esp. the different syntaxes ppl have preferred) and, more importantly, _what are you trying to add to the discussion_. So far, it's still a repeat of past threads.

Edit:

Oh I see, you now add implementations. Still, it'd be nice if you discuss alternate spellings in the proposal (and why yours are superior).

---

<div class="post-metadata">

**Author:** ![GreatApe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/greatape/32/2640_2.png) [@GreatApe](https://forums.swift.org/u/GreatApe)\
**Post date:** [December 23, 2021, 2:11pm UTC](https://forums.swift.org/t/if-let-shorthand/54230/23 "2021-12-23T14:11:51Z")

</div>

> [@bzamayo](#):
>
> typing out that the same thing equals the same thing

That's the thing though. The left hand side is very different from the right hand side, which you'll notice if you use a new name.

I think something like `if shadow` makes much more sense. `if let x { }` looks fundamentally different from `if let x = y { }` even though they do the same thing. They both unwrap and assign a value, the difference is that the latter doesn't shadow the name.

But at the same time I don't think we need more syntax if we can avoid it.

---

<div class="post-metadata">

**Author:** ![Alejandro\_Martinez](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alejandro_martinez/32/821_2.png) [@Alejandro\_Martinez](https://forums.swift.org/u/Alejandro_Martinez)\
**Post date:** [December 23, 2021, 2:15pm UTC](https://forums.swift.org/t/if-let-shorthand/54230/24 "2021-12-23T14:15:51Z")

</div>

I appreciate the effort of implementing this but I'm still not convinced we need it. `if let x { ` looks fundamentally broken, what is a let binding doing on its own? If instead we were proposing to get semantic analysis (for example like typescript) it would be another story.

That said I imagine I wouldn't opposite very strongly if this ended up being officially proposed.

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [December 23, 2021, 2:27pm UTC](https://forums.swift.org/t/if-let-shorthand/54230/25 "2021-12-23T14:27:37Z")

</div>

> [@cal](#):
>
> Reducing duplication, especially of lengthy variable names, makes code both easier to write _and_ easier to read.
> 
> For example, this statement that unwraps `someLengthyVariableName` and `anotherImportantVariable` is rather arduous to read (and was without a doubt arduous to write):
> 
> ```swift
> let someLengthyVariableName: Foo? = ...
> let anotherImportantVariable: Bar? = ...
> 
> if let someLengthyVariableName = someLengthyVariableName, let anotherImportantVariable = anotherImportantVariable {
> ...
> }
> 
> ```

I'm not against this pitch in the form it is presented. Would just note that in this example:

```
if let someLengthyVariableName = someLengthyVariableName, let anotherImportantVariable = anotherImportantVariable {
    foo(someLengthyVariableName)
    anotherImportantVariable.bar(someLengthyVariableName)
}

```

rather than writing:

```
if let someLengthyVariableName, let anotherImportantVariable {
    foo(someLengthyVariableName)
    anotherImportantVariable.bar(someLengthyVariableName)
}

```

I would probably prefer something more concise anyway:

```
if let a = someLengthyVariableName, let b = anotherImportantVariable {
    foo(a)
    b.bar(a)
}

```

---

<div class="post-metadata">

**Author:** ![Avi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/avi/32/2962_2.png) [@Avi](https://forums.swift.org/u/Avi)\
**Post date:** [December 23, 2021, 2:32pm UTC](https://forums.swift.org/t/if-let-shorthand/54230/26 "2021-12-23T14:32:37Z")

</div>

One also misses out on the ability to "jump to definition" (in Xcode) to locate the declaration for the RHS.

---

<div class="post-metadata">

**Author:** ![mickeyl](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mickeyl/32/12258_2.png) [@mickeyl](https://forums.swift.org/u/mickeyl)\
**Post date:** [December 23, 2021, 2:35pm UTC](https://forums.swift.org/t/if-let-shorthand/54230/27 "2021-12-23T14:35:49Z")

</div>

As someone always using the explicit `self`, I don't think I would use this shortcut.

For me, `if let foo = self.foo` is crystal clear and understandable even if you look at it years later. I don't mind repeating myself here.

---

<div class="post-metadata">

**Author:** ![1-877-547-7272](https://avatars.discourse-cdn.com/v4/letter/1/278dde/32.png) [@1-877-547-7272](https://forums.swift.org/u/1-877-547-7272)\
**Post date:** [December 23, 2021, 2:37pm UTC](https://forums.swift.org/t/if-let-shorthand/54230/28 "2021-12-23T14:37:29Z")

</div>

> [@tera](#):
>
> I would probably prefer something more concise anyway:
> 
> ```swift
> if let a = someLengthyVariableName, let b = anotherImportantVariable {
> foo(a)
> b.bar(a)
> }
> 
> ```

IMO this is one of the strengths of this proposal. Variable names like `a` or `b` are cryptic and vague, which contradicts Swift’s goal of clarity at the point of use.

---

<div class="post-metadata">

**Author:** ![cal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cal/32/35557_2.png) [@cal](https://forums.swift.org/u/cal)\
**Post date:** [December 23, 2021, 2:43pm UTC](https://forums.swift.org/t/if-let-shorthand/54230/29 "2021-12-23T14:43:29Z")

</div>

> [@jeremyabannister](#):
>
> Have we considered the idea of introducing a new `unwrap` keyword?
> 
> ```swift
> var foo: Int?
> 
> unwrap foo {
> // `foo` has been shadowed by the unwrapped value
> }
> 
> ```

Some thoughts:

1. The new syntax should be usable anywhere you can use `if let foo = foo` conditions today (`if`, `else`, `guard`, `while`), so if using `unwrap` we would likely want this to be:

2. The new syntax should support the distinction between `let foo = foo` and `var foo = foo`. Including that would give us something like:

3. I personally think its preferable to draw from existing keywords and patterns rather than introduce a new keyword for this. I think `if let` follows naturally from the existing syntax, like how the existing syntax follows naturally from the long-form `if case .some` syntax:

---

<div class="post-metadata">

**Author:** ![Avi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/avi/32/2962_2.png) [@Avi](https://forums.swift.org/u/Avi)\
**Post date:** [December 23, 2021, 2:53pm UTC](https://forums.swift.org/t/if-let-shorthand/54230/30 "2021-12-23T14:53:58Z")

</div>

> [@1-877-547-7272](#):
>
> Variable names like `a` or `b` are cryptic and vague, which contradicts Swift’s goal of clarity at the point of use.

If `a` or `b` are too vague in this context, your `if` block is way too large for readability.

---

<div class="post-metadata">

**Author:** ![pilky](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pilky/32/7636_2.png) [@pilky](https://forums.swift.org/u/pilky)\
**Post date:** [December 23, 2021, 3:19pm UTC](https://forums.swift.org/t/if-let-shorthand/54230/31 "2021-12-23T15:19:29Z")

</div>

So generally I’m very supportive of anything making it easier to deal with optionals. However, I would want to know such a feature would not complicate or preclude a more comprehensive form of nil checking (as listed in the alternatives), as I feel that is something Swift needs. If this did restrict such future options I would give it a -1. If it works as a first step towards such nil checking then it's a +1

---

<div class="post-metadata">

**Author:** ![christianselig](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/christianselig/32/9809_2.png) [@christianselig](https://forums.swift.org/u/christianselig)\
**Post date:** [December 23, 2021, 3:27pm UTC](https://forums.swift.org/t/if-let-shorthand/54230/32 "2021-12-23T15:27:54Z")

</div>

I think naming things short names that are no longer descriptive/indicative of what the variables do purely so you can save time writing unnecessarily verbose checks is a perfect example of why this proposal is needed.

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [December 23, 2021, 3:41pm UTC](https://forums.swift.org/t/if-let-shorthand/54230/33 "2021-12-23T15:41:59Z")

</div>

I understand this is not everyone's cup of tea, but choosing between these two examples my personal preference would be for the latter:

```swift
if let someLengthyVariableName, let anotherImportantVariable {
    foo(someLengthyVariableName)
    anotherImportantVariable.bar(someLengthyVariableName)
}

```

I would probably prefer something more concise anyway:

```swift
if let a = someLengthyVariableName, let b = anotherImportantVariable {
    foo(a)
    b.bar(a)
}

```

perhaps not "a" and "b" per se, but normally I don't afraid of using names like "x" or "y" or "i" or "n" or "v" or "vc", or "nav", etc, as @Avi has pointed out the short context is the key ("7 line rule", etc).

Having said that, I still see the value of the proposal, just for me a better convincing example would be something with short names like:

```
if let one, let two {
    foo(one)
    two.bar(one)
}

```

rather than the one that uses very long names (in which case this proposal would be of no help for me).

"Clarity at the point of use" includes using short names, long names that are trying to be a mini description of what the thing is are way too noisy for me personally to be useful.

---

<div class="post-metadata">

**Author:** ![James\_Dempsey](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/james_dempsey/32/14336_2.png) [@James\_Dempsey](https://forums.swift.org/u/James_Dempsey)\
**Post date:** [December 23, 2021, 3:50pm UTC](https://forums.swift.org/t/if-let-shorthand/54230/34 "2021-12-23T15:50:25Z")

</div>

For me, it reads ambiguously because `foo` could be a Bool. I need to figure out the type of 'foo' before I understand whether the if statement is evaluating `foo` or creating an unwrapped constant `foo`. In the for-in case, it's not ambiguous.

---

<div class="post-metadata">

**Author:** ![Objective\_Cesar](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/objective_cesar/32/18310_2.png) [@Objective\_Cesar](https://forums.swift.org/u/Objective_Cesar)\
**Post date:** [December 23, 2021, 4:05pm UTC](https://forums.swift.org/t/if-let-shorthand/54230/35 "2021-12-23T16:05:00Z")

</div>

I think this looks a bit broken at first. Defining a let without assigning anything to it seems odd to me personally. But I guess I could adopt to it if I have to.

---

<div class="post-metadata">

**Author:** ![hassila](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hassila/32/22647_2.png) [@hassila](https://forums.swift.org/u/hassila)\
**Post date:** [December 23, 2021, 5:55pm UTC](https://forums.swift.org/t/if-let-shorthand/54230/36 "2021-12-23T17:55:29Z")

</div>

It could even be an Optional bool 😉

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [December 23, 2021, 6:50pm UTC](https://forums.swift.org/t/if-let-shorthand/54230/37 "2021-12-23T18:50:58Z")

</div>

> [@cal](#):
>
> ```swift
> else let foo { ... }
> else var foo { ... }
> 
> ```

Presumably these ought to be `else if`.

---

<div class="post-metadata">

**Author:** ![cal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cal/32/35557_2.png) [@cal](https://forums.swift.org/u/cal)\
**Post date:** [December 23, 2021, 6:52pm UTC](https://forums.swift.org/t/if-let-shorthand/54230/38 "2021-12-23T18:52:56Z")

</div>

Haha, oops! Fixed, thanks 👍

---

<div class="post-metadata">

**Author:** ![aerobounce](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/aerobounce/32/14175_2.png) [@aerobounce](https://forums.swift.org/u/aerobounce)\
**Post date:** [December 23, 2021, 7:57pm UTC](https://forums.swift.org/t/if-let-shorthand/54230/39 "2021-12-23T19:57:16Z")

</div>

@cal

Happy to see this going forward.  
Because I never omit type declaration, may I ask if this expression allowed with current implementation?

```swift
var someLengthyVariableName: SomeLengthyTypeName?

if let someLengthyVariableName: SomeLengthyTypeName {
    ...
}

```

---

<div class="post-metadata">

**Author:** ![cal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cal/32/35557_2.png) [@cal](https://forums.swift.org/u/cal)\
**Post date:** [December 23, 2021, 7:58pm UTC](https://forums.swift.org/t/if-let-shorthand/54230/40 "2021-12-23T19:58:13Z")

</div>

Yes! That's supported by the current implementation.

[Previous page](https://forums.swift.org/t/if-let-shorthand/54230.md?page=1)

[Next page](https://forums.swift.org/t/if-let-shorthand/54230.md?page=3)
