`if let` shorthand

Some thoughts: if you assume that the ref and inout introducers discussed here will be added to the language, it feels very natural for this style of shorthand syntax to support those introducers as well:

// "Existing" optional binding condition syntax
if let foo = foo { ... }
if var foo = foo { ... }
if ref foo = foo { ... }
if inout foo = &foo { ... }

// Shorthand syntax
if let foo { ... }
if var foo { ... }
if ref foo { ... }
if inout &foo { ... }

In the same way that let / var introducers are currently interchangeable throughout the language, I'd expect this to be the case with hypothetical ref and inout introducers as well -- all of these introducers will likely always have reasonable, distinct use cases (right?). It seems like we would want to preserve that consistency and support all of them here as well.

One difference is that the new introducers would enforce exclusivity (right?), so in the future it may make sense to permit something like:

// foo.bar is optional
if inout &foo.bar {
  // foo.bar is non-optional
  // this is safe because the inout borrow guarantees exclusive access
}

Supporting this for inout mutable borrows doesn't necessarily mean that also supporting a more limited form of this feature for let / var is harmful. As long as let and var exist, it seems reasonable to permit authors to use them in an ergonomic way.

4 Likes