`if let` shorthand

Speaking personally, I think using $0 for this would be a bad idea. $0 is only useful because it enables extremely terse closures. These are good in moderation when they are so short and simple that it's obvious what is going on. But as soon as your closure gets more than trivial, you should name the arguments.

But when you do, those arguments are at least not repeated in a way that adds noise. It is a.reduce(x) { result, element in ... }, not a.reduce(x) { result = result, element = element in ... }. In a way, this proposal aims to achieve similar clarity and concision for if let.

  if let element.optionalProperty {
   // What even happens now?
 }

There are two very reasonable answers: either this declares a local variable called optionalProperty, or it doesn't compile. Both would be fine IMO.

11 Likes