I believe allowing this would give developers an easy path to sacrifice clarity for brevity, and so should not compile.
In some very common cases, such as the first
property of collections, the automatic name would almost never be a clear one, but would definitely be shorter to type:
guard let customers.first else { return }
// 'first' is the easiest name, but almost never a good name
print(first.firstName, first.lastName)
It also sets up the potential of conflicting implied names, for example:
guard let budgeted.total, let actual.total else { return }
or
guard let locales.first, let languages.first else { return }
Where the shorthand works on the first use but not the second.
The compiler could potentially work around this by creating up an automatic variable name such as budgetedTotal
, actualTotal
, localesFirst
, and languagesFirst
.
In some cases, that can lead to a descriptive variable name, in some cases like localesFirst
, not a great name.
Finally, the construct if let locales.first
is not declaring something called locales.first
, whereas the shorthand as pitched, if let foo
, is declaring a new variable foo
, so stays true to the meaning of let
. It's the right hand side of the current expression that is omitted with the shorthand, not the left hand side.
So, I hope this usage would be disallowed.