[swift-evolution] Change default compiler fix for not-unwrapped Optional from ! To ?

Chris Lattner clattner at nondot.org
Tue Jul 18 20:50:42 CDT 2017


> On Jul 13, 2017, at 10:21 PM, Félix Cloutier via swift-evolution <swift-evolution at swift.org> wrote:
> 
> How should the fix-it transform the following code to make it valid?
> 
> func foo(bar: String) -> Int
> func frob() -> String?
> 
> func qaz() -> Int {
> 	return foo(bar: frob()) // frob() is optional, insert a ! to fix
> }

You rewrite it to:

	return foo(bar: frob() ?? <#value if nil#>)

<# #> are the ascii representation of code placeholder tokens in Xcode.

-Chris

> 
>> Le 13 juil. 2017 à 21:36, Robert Bennett via swift-evolution <swift-evolution at swift.org> a écrit :
>> 
>> When writing Swift code, it is not uncommon to forget to unwrap optionals. The compiler will offer a fixit, telling you you must insert either a ? or a !. However, when you accept the fixit, ! is inserted (at least, it is for me in Xcode 8.3).
>> 
>> Ideally the fixit would default to ? because this is the preferred option; ! is often a sign of non-Swifty code and does not interact well with idiomatic Swift constructs such as if-let-(as?), guard-let-(as?)-else, etc. Also I think it’s safe to say that fixits should not err on the side of crashing at runtime.
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution



More information about the swift-evolution mailing list