[swift-evolution] Please remove obscure "!" requirements for implicitly unwrapped optionals

Per Melin p at greendale.se
Fri Dec 4 19:00:58 CST 2015


On Sat, Dec 5, 2015 at 1:04 AM, Chris Lattner <clattner at apple.com> wrote:

>
> On Dec 4, 2015, at 1:18 PM, Per Melin <p at greendale.se> wrote:
>
> To be explicit:
>
>     let a: Bool! = false
>
>     if a == false { }  // Works as expected
>
>     if !a { }  // Works as expected
>
>     if a { }   // "error: optional type 'Bool!' cannot be used as a
> boolean; test for '!= nil' instead"
>
> Xcode "fixes" the last line like this:
>
>     if (a != nil) { }
>
>
> Huh ok.  Out of curiosity, why are you using IUOs for this?
>

I will leave that question to Amir who created this thread. I try to avoid
IUOs altogether. I only found the above when trying to figure out what the
heck he was talking about.



> I’m interested in (continuing to ) dramatically reduce the prevalence of
> IOUs in code (eliminating the need to use them), and reduce their power in
> various ways.  IUOs are one of the biggest sources of surprising behavior
> in Swift, and while they were a necessary feature to start with, their
> importance is going down over time.
>

What surprised me was the IIUOs. I did not expect Swift to happily help me
shoot myself in the foot quite so easily as this.

func isNegative(i: Int?) -> Bool {
    return i < 0
}

(No, I have never actually written code like this. I can't tell how likely
it is that I would make this mistake.)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151205/53b552a3/attachment.html>


More information about the swift-evolution mailing list