March 2017
Intermediate to advanced
226 pages
5h 13m
English
I rarely use optional mapping in condition clauses. I think they’re confusing to most readers. Prefer clarity even when that causes you to bind an extra variable, as in the following example. Here, the key unwraps and shadows a variable defined as part of the problem statement. This is entirely safe:
| | if let value = key.flatMap({ dict[$0] }) ... // no |
| | if let key = key, value = dict[key] ... // yes |
If you rephrase the call away from the let x = x pattern, as in the following snippet, you may shadow an existing variable. This would cause a conflict in the if statement’s success clause. It’s an unlikely scenario but it’s one you should consider. The non-shadowing optional-mapping alternative presents no ...
Read now
Unlock full access