<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 <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> 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"</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>
> Just to clarify : it will crash not because `nil` passed, but because there<br>> is no check for `nil` in the function itself. I.e. nobody prevents you to<br>> make a function that *will* check the nil and at the same time use all the<br>> bonuses from implicitly unwrapped optional parameter:<br>> <br>> func foo(_ i: Int!) {<br>> guard i != nil else {return}<br>> <br>> print(i) // use IUO parameter<br>> }<br>> <br>> foo(nil) // no errors<br>> foo(1) // 1<br>> <br>> <br>> On 09.06.2016 11:04, Spromicky via swift-evolution wrote:<br>> > 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>> > 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">> > <br>> > > This confused me at the beginning.<br>> > > <br>> > > 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>> > > <br>> > > --<br>> > > J. Charles<br>> > > <br>> > > > Le 8 juin 2016 à 13:30, Spromicky via swift-evolution<<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>>a écrit :<br>> > > > <br>> > > > Hello, everyone!<br>> > > > <br>> > > > 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>> > > > People who new in swift look at how they old Obj-C code (without nullability modifiers) translate in to swift:<br>> > > > <br>> > > > Obj-C:<br>> > > > - (void)foo:(NSInteger)bar {<br>> > > > //...<br>> > > > }<br>> > > > <br>> > > > Swift transaliton:<br>> > > > func foo(bar: Int!) {<br>> > > > //...<br>> > > > }<br>> > > > <br>> > > > And think that force unwrapping in signature is good practice. And start write functions in clear swift code like this:<br>> > > > <br>> > > > func newFoo(bar: Int!) {<br>> > > > //...<br>> > > > }<br>> > > > <br>> > > > and use it like this:<br>> > > > <br>> > > > let bar: Int? = 1<br>> > > > newFoo(bar)<br>> > > > <br>> > > > And it really work, and they does not think that this can crash in case if `bar` will be `nil`.<br>> > > > But in clear swift we wanna work with parametrs in function that clearly or optional, or not.<br>> > > > <br>> > > > func newFoo(bar: Int) {<br>> > > > //...<br>> > > > }<br>> > > > <br>> > > > or<br>> > > > <br>> > > > func newFoo(bar: Int?) {<br>> > > > //...<br>> > > > }<br>> > > > <br>> > > > When we write a new function we know what we need in this case and use optional params or not.<br>> > > > <br>> > > > So my proposal is remove force unwrapping(`!`) from function signatures, cause it have no sense, and that confuse new users.<br>> > > > _______________________________________________<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" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>> > _______________________________________________<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" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>> <br>> <br>><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>