[swift-evolution] Swift null safety questions

Elijah Johnson ejrx7753 at gmail.com
Tue Mar 7 12:36:08 CST 2017


How about the proposal of warnings and/or fix-it for unnecessary use of
optionals?

It seems to me though, that there aren't too many "necessary" uses for
forced-unwrapping. Judging from (
https://github.com/apple/swift-evolution/blob/master/proposals/0054-abolish-iuo.md),
it was probably added as a way of helping work with Obj-C also.

func test(a:String?) -> String {
    return a!
}

is equivelent to

func test(a:String?) -> String {
    if let a = a {
         print(a);
    } else {
         fatalError();
    }
}

I wouldn't remove it at this stage, but to quote (
https://github.com/apple/swift-evolution/blob/master/commonly_proposed.md),
it is "favoring terseness over clarity by introducing new magic syntactic
sugar," ironically adding this very terse and easy syntax for the coding
style that we do not want to incentivize.

Adding warnings and a fix-it to make the crash explicit would more or less
be as good as removing forced-unwrap from the language. Just maybe needs
some clarification of what exactly is a valid use of the force-unwrap.
Maybe for interop with Obj-C it should be considered "necessary", for
example.

2017-03-07 11:54 GMT-05:00 Josh Parmenter <jparmenter at vectorform.com>:

>
> On Mar 6, 2017, at 7:52 PM, Elijah Johnson via swift-evolution <
> swift-evolution at swift.org<mailto:swift-evolution at swift.org>> wrote:
>
> Says: Fix it: Insert !
> Should say: Fix it: Use optional binding.
>
> I agree with this in general. I tell people that I’m teaching that
> unwrapping uses ‘!' for a reason. It should scream at you that something is
> happening that probably needs more careful attention.
>
> Best,
> Josh
>
>
> Joshua Parmenter | Engineering Lead, Apple Technologies
>
> T 248 777 7777
> C 206 437 1551
> F 248 616 1980
> www.vectorform.com<http://www.vectorform.com/>
>
> Vectorform
> 2107 Elliott Ave Suite 303
> Seattle, WA  98121 USA
>
> Think Tank. Lab. Studio.
> We invent digital products and experiences.
>
> SEATTLE | DETROIT | NEW YORK | MUNICH | HYDERABAD
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170307/12abf135/attachment.html>


More information about the swift-evolution mailing list