<div dir="ltr">Yes, that’s exactly my point. Force unwrapping optionals adds confusion for new users; all too often I see newcomers ending up with the assumption that the force unwrapping takes care of the check for them.<br><br><div class="gmail_quote"><div dir="ltr">On Thu, Jun 9, 2016 at 6:40 AM Spromicky via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>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>Non-optional:</div><div><div>func foo(_ bar: Int) {</div><div>    //...</div><div>}</div></div><div>Clear, good!</div><div><br></div><div>Now try with optional: </div><div><div>func foo(_ bar: Int?) {</div><div>    guard let bar = bar else { return }</div><div><br></div><div>    //...</div><div>}</div></div><div>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. </div><div>Oh! We can set params force unwrap! What it mean? And how it works?</div><div><br></div><div><div>func foo(_ bar: Int!) {</div><div>    //...</div><div>}</div></div><div><br></div><div>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><br></div><div><div>func foo(_ bar: Int!) {</div></div><div><div>    guard bar != nil else { return }</div></div><div><br></div><div><div>    //...</div><div>}</div></div><div><br></div><div>Oh it is looks safety. But what difference from usual usage of optional?<b> </b>Just only that we not write `let bar = bar` in `guard`?</div><div>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&quot;</div><div>This two cases with force unwrap we can write in other way: </div><div>func foo(_ bar: Int) {</div><div>    //...</div><div>}</div><div><br></div><div>var i: Int?</div><div>//...</div><div>foo(i!)</div><div><br></div><div>or </div><div><br></div><div><div>func foo(_ bar: Int) {</div><div>    //...</div><div>}</div><div><br></div><div>var i: Int?</div><div>//...</div><div>if let i = i {</div><div>    foo(i)</div><div>}</div></div><div><br></div><div>But now we fully and clearly understand what happening.“</div><div><br></div><div>I hope I explained my point. Just wanna add more safety and clarity.</div></div><div style="word-wrap:break-word"><div><br></div>
&gt; Just to clarify : it will crash not because `nil` passed, but because there<br>&gt; is no check for `nil` in the function itself. I.e. nobody prevents you to<br>&gt; make a function that *will* check the nil and at the same time use all the<br>&gt; bonuses from implicitly unwrapped optional parameter:<br>&gt; <br>&gt; func foo(_ i: Int!) {<br>&gt; guard i != nil else {return}<br>&gt; <br>&gt; print(i) // use IUO parameter<br>&gt; }<br>&gt; <br>&gt; foo(nil) // no errors<br>&gt; foo(1) // 1<br>&gt; <br>&gt; <br>&gt; On 09.06.2016 11:04, Spromicky via swift-evolution wrote:<br>&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>&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></div></blockquote><div><br></div><div>Yes, this is probably the only reason why it was added. The best solution probably to convert all of these methods to allowing Optional types by default and then going through them and adding `_Nonnull` on a case-by-case basis.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">&gt; &gt; <br>&gt; &gt; &gt; This confused me at the beginning.<br>&gt; &gt; &gt; <br>&gt; &gt; &gt; But doesn&#39;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>&gt; &gt; &gt; <br>&gt; &gt; &gt; --<br>&gt; &gt; &gt; J. Charles<br>&gt; &gt; &gt; <br>&gt; &gt; &gt; &gt; Le 8 juin 2016 à 13:30, Spromicky via swift-evolution&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;a écrit :<br>&gt; &gt; &gt; &gt; <br>&gt; &gt; &gt; &gt; Hello, everyone!<br>&gt; &gt; &gt; &gt; <br>&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>&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>&gt; &gt; &gt; &gt; <br>&gt; &gt; &gt; &gt; Obj-C:<br>&gt; &gt; &gt; &gt; - (void)foo:(NSInteger)bar {<br>&gt; &gt; &gt; &gt; //...<br>&gt; &gt; &gt; &gt; }<br>&gt; &gt; &gt; &gt; <br>&gt; &gt; &gt; &gt; Swift transaliton:<br>&gt; &gt; &gt; &gt; func foo(bar: Int!) {<br>&gt; &gt; &gt; &gt; //...<br>&gt; &gt; &gt; &gt; }<br>&gt; &gt; &gt; &gt; <br>&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>&gt; &gt; &gt; &gt; <br>&gt; &gt; &gt; &gt; func newFoo(bar: Int!) {<br>&gt; &gt; &gt; &gt; //...<br>&gt; &gt; &gt; &gt; }<br>&gt; &gt; &gt; &gt; <br>&gt; &gt; &gt; &gt; and use it like this:<br>&gt; &gt; &gt; &gt; <br>&gt; &gt; &gt; &gt; let bar: Int? = 1<br>&gt; &gt; &gt; &gt; newFoo(bar)<br>&gt; &gt; &gt; &gt; <br>&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>&gt; &gt; &gt; &gt; But in clear swift we wanna work with parametrs in function that clearly or optional, or not.<br>&gt; &gt; &gt; &gt; <br>&gt; &gt; &gt; &gt; func newFoo(bar: Int) {<br>&gt; &gt; &gt; &gt; //...<br>&gt; &gt; &gt; &gt; }<br>&gt; &gt; &gt; &gt; <br>&gt; &gt; &gt; &gt; or<br>&gt; &gt; &gt; &gt; <br>&gt; &gt; &gt; &gt; func newFoo(bar: Int?) {<br>&gt; &gt; &gt; &gt; //...<br>&gt; &gt; &gt; &gt; }<br>&gt; &gt; &gt; &gt; <br>&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>&gt; &gt; &gt; &gt; <br>&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>&gt; &gt; &gt; &gt; _______________________________________________<br>&gt; &gt; &gt; &gt; swift-evolution mailing list<br>&gt; &gt; &gt; &gt; <a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>&gt; &gt; &gt; &gt; <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>&gt; &gt; _______________________________________________<br>&gt; &gt; swift-evolution mailing list<br>&gt; &gt; <a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>&gt; &gt; <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>&gt; <br>&gt; <br>&gt;<span> </span>

</div>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div></div><div dir="ltr">-- <br></div><div data-smartmail="gmail_signature"><div dir="ltr">-Saagar Jha</div></div>