<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Sure, and this so confused. For new users its seems like: “Ok, we have optional and non-optional variables in Swift. We can force unwrap optional if we want, or if we sure, that in this moment this variable not `nil`. Now we try write functions.</div><div class="">Non-optional:</div><div class=""><div class="">func foo(_ bar: Int) {</div><div class="">&nbsp; &nbsp; //...</div><div class="">}</div></div><div class="">Clear, good!</div><div class=""><br class=""></div><div class="">Now try with optional:&nbsp;</div><div class=""><div class="">func foo(_ bar: Int?) {</div><div class="">&nbsp; &nbsp; guard let bar = bar else { return }</div><div class=""><br class=""></div><div class="">&nbsp; &nbsp; //...</div><div class="">}</div></div><div class="">Ok, we can pass optional value in function and implement some default behaviour for `nil`, for example, or whatever. It is have sense. It is cool. It is safety.&nbsp;</div><div class="">Oh! We can set params force unwrap! What it mean? And how it works?</div><div class=""><br class=""></div><div class=""><div class="">func foo(_ bar: Int!) {</div><div class="">&nbsp; &nbsp; //...</div><div class="">}</div></div><div class=""><br class=""></div><div class="">Hmmmm… There will be crash if we use `bar` and it will be `nil`. So we must check for `nil` before use. Ok.</div><div class=""><br class=""></div><div class=""><div class="">func foo(_ bar: Int!) {</div></div><div class=""><div class="">&nbsp; &nbsp; guard bar != nil else { return }</div></div><div class=""><br class=""></div><div class=""><div class="">&nbsp; &nbsp; //...</div><div class="">}</div></div><div class=""><br class=""></div><div class="">Oh it is looks safety. But what difference from usual usage of optional?<b class=""> </b>Just only that we not write `let bar = bar` in `guard`?</div><div class="">And if someone else will use my function and will sees in param list force unwrap what he must think? That mean that he can pass optional and all will be checked and fine? Or it mean “yes, you can pass optional, but it was makes just for comfort pass optional without unwrapping, but still not have checks in implementation"</div><div class="">This two cases with force unwrap we can write in other way:&nbsp;</div><div class="">func foo(_ bar: Int) {</div><div class="">&nbsp; &nbsp; //...</div><div class="">}</div><div class=""><br class=""></div><div class="">var i: Int?</div><div class="">//...</div><div class="">foo(i!)</div><div class=""><br class=""></div><div class="">or&nbsp;</div><div class=""><br class=""></div><div class=""><div class="">func foo(_ bar: Int) {</div><div class="">&nbsp; &nbsp; //...</div><div class="">}</div><div class=""><br class=""></div><div class="">var i: Int?</div><div class="">//...</div><div class="">if let i = i {</div><div class="">&nbsp; &nbsp; foo(i)</div><div class="">}</div></div><div class=""><br class=""></div><div class="">But now we fully and clearly understand what happening.“</div><div class=""><br class=""></div><div class="">I hope I explained my point. Just wanna add more safety and clarity.</div><div class=""><br class=""></div>
&gt; Just to clarify : it will crash not because `nil` passed, but because there<br class="">&gt; is no check for `nil` in the function itself. I.e. nobody prevents you to<br class="">&gt; make a function that *will* check the nil and at the same time use all the<br class="">&gt; bonuses from implicitly unwrapped optional parameter:<br class="">&gt; <br class="">&gt; func foo(_ i: Int!) {<br class="">&gt; guard i != nil else {return}<br class="">&gt; <br class="">&gt; print(i) // use IUO parameter<br class="">&gt; }<br class="">&gt; <br class="">&gt; foo(nil) // no errors<br class="">&gt; foo(1) // 1<br class="">&gt; <br class="">&gt; <br class="">&gt; On 09.06.2016 11:04, Spromicky via swift-evolution wrote:<br class="">&gt; &gt; Yep, but no one stop you if you pass optional value. And in runtime it crash in case if it really will be `nil`. But from other side non-optional params make the same, and does not let you pass an optional value, and show error on compile time. This make your code more safety.<br class="">&gt; &gt; Now force unwrapped params in function used only if you use old Obj-C code without nullability modifiers, so no sense, i think, for clear swift code.<br class="">&gt; &gt; <br class="">&gt; &gt; &gt; This confused me at the beginning.<br class="">&gt; &gt; &gt; <br class="">&gt; &gt; &gt; But doesn't Int! In parameter type means the function is awaiting an unwrapped value so the user should ensure that it data parameter is available, valid, and unwrapped?<br class="">&gt; &gt; &gt; <br class="">&gt; &gt; &gt; --<br class="">&gt; &gt; &gt; J. Charles<br class="">&gt; &gt; &gt; <br class="">&gt; &gt; &gt; &gt; Le 8 juin 2016 à 13:30, Spromicky via swift-evolution&lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt;a écrit :<br class="">&gt; &gt; &gt; &gt; <br class="">&gt; &gt; &gt; &gt; Hello, everyone!<br class="">&gt; &gt; &gt; &gt; <br class="">&gt; &gt; &gt; &gt; I wanna propose to you to remove force unwrapping in fuction signature for swift code. That no sense in clear swift code. If we wanna use some optional value as function param, that is not optional, we must unwrap it before function call.<br class="">&gt; &gt; &gt; &gt; People who new in swift look at how they old Obj-C code (without nullability modifiers) translate in to swift:<br class="">&gt; &gt; &gt; &gt; <br class="">&gt; &gt; &gt; &gt; Obj-C:<br class="">&gt; &gt; &gt; &gt; - (void)foo:(NSInteger)bar {<br class="">&gt; &gt; &gt; &gt; //...<br class="">&gt; &gt; &gt; &gt; }<br class="">&gt; &gt; &gt; &gt; <br class="">&gt; &gt; &gt; &gt; Swift transaliton:<br class="">&gt; &gt; &gt; &gt; func foo(bar: Int!) {<br class="">&gt; &gt; &gt; &gt; //...<br class="">&gt; &gt; &gt; &gt; }<br class="">&gt; &gt; &gt; &gt; <br class="">&gt; &gt; &gt; &gt; And think that force unwrapping in signature is good practice. And start write functions in clear swift code like this:<br class="">&gt; &gt; &gt; &gt; <br class="">&gt; &gt; &gt; &gt; func newFoo(bar: Int!) {<br class="">&gt; &gt; &gt; &gt; //...<br class="">&gt; &gt; &gt; &gt; }<br class="">&gt; &gt; &gt; &gt; <br class="">&gt; &gt; &gt; &gt; and use it like this:<br class="">&gt; &gt; &gt; &gt; <br class="">&gt; &gt; &gt; &gt; let bar: Int? = 1<br class="">&gt; &gt; &gt; &gt; newFoo(bar)<br class="">&gt; &gt; &gt; &gt; <br class="">&gt; &gt; &gt; &gt; And it really work, and they does not think that this can crash in case if `bar` will be `nil`.<br class="">&gt; &gt; &gt; &gt; But in clear swift we wanna work with parametrs in function that clearly or optional, or not.<br class="">&gt; &gt; &gt; &gt; <br class="">&gt; &gt; &gt; &gt; func newFoo(bar: Int) {<br class="">&gt; &gt; &gt; &gt; //...<br class="">&gt; &gt; &gt; &gt; }<br class="">&gt; &gt; &gt; &gt; <br class="">&gt; &gt; &gt; &gt; or<br class="">&gt; &gt; &gt; &gt; <br class="">&gt; &gt; &gt; &gt; func newFoo(bar: Int?) {<br class="">&gt; &gt; &gt; &gt; //...<br class="">&gt; &gt; &gt; &gt; }<br class="">&gt; &gt; &gt; &gt; <br class="">&gt; &gt; &gt; &gt; When we write a new function we know what we need in this case and use optional params or not.<br class="">&gt; &gt; &gt; &gt; <br class="">&gt; &gt; &gt; &gt; So my proposal is remove force unwrapping(`!`) from function signatures, cause it have no sense, and that confuse new users.<br class="">&gt; &gt; &gt; &gt; _______________________________________________<br class="">&gt; &gt; &gt; &gt; swift-evolution mailing list<br class="">&gt; &gt; &gt; &gt; <a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">&gt; &gt; &gt; &gt; <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">&gt; &gt; _______________________________________________<br class="">&gt; &gt; swift-evolution mailing list<br class="">&gt; &gt; <a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">&gt; &gt; <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="">&gt; <br class="">&gt; <br class="">&gt;<span class="Apple-converted-space">&nbsp;</span>

</body></html>