There is no “good” reason for the current behavior that I know of.
The bad reasons are:
- we want to produce a fixit hint for this, because it is a common problem
- producing a ! is useful (even if you ignore it and manually use a ?) because it explains where the optional is in a very concrete way.
- doing the analysis to determine where to put a ? is extremely tricky in some cases, and it is very bad to suggest a fixit that won’t work.
- Using ? is only solution to the problem, you may want to use (e.g.) "?? defaultValue”
That said, any improvements to this area would be greatly welcomed by everyone :-)
On Feb 26, 2016, at 3:54 PM, Daniel Duan via swift-dev <email@example.com> wrote:
Currently, when accessing a property of an optional value without unwrapping,
the compiler issues a fixit message that adds a "!". This seems very
unintuitive as force unwrapping is usually unsafe.
I also recall that "?" was issued in the past.
Is there a particular reason for not to suggest "?" instead? E.g. doing so
will result in semantically incorrect expressions in certain scenario? If not,
I'd love to contribute a patch that brings "?" fixit back.