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