<div dir="ltr"><div class="gmail_default" style="font-family:trebuchet ms,sans-serif">I too agree the unwrapping process can be tedious but I believe this is best because it forces us to check we&#39;re doing the right thing in code and prevents a lot of errors we have to keep on checking in more dynamic languages. -1 from me too.</div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature"><div dir="ltr">- Leonardo</div></div></div>
<br><div class="gmail_quote">On 11 May 2016 at 10:19, Patrick Smith via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Ha great stuff Basem! Glad we could help :) Look forward to your next proposal!<br>
<div class="HOEnZb"><div class="h5"><br>
&gt; On 11 May 2016, at 11:17 PM, Basem Emara &lt;<a href="mailto:contact@basememara.com">contact@basememara.com</a>&gt; wrote:<br>
&gt;<br>
&gt; Thanks for the input everybody, and for the deeper analysis, Rod.<br>
&gt;<br>
&gt; It’s set straight in my mind now and have even a greater appreciation of optionals than before thanks to this discussion. Let’s scrape this pitch :)<br>
&gt;<br>
&gt; Happy coding! -Basem<br>
&gt;<br>
&gt;&gt; On May 11, 2016, at 9:10 AM, Rod Brown &lt;<a href="mailto:rodney.brown6@icloud.com">rodney.brown6@icloud.com</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; I think what you’re referring to as default values would be what you get if you initialize the type directly.<br>
&gt;&gt;<br>
&gt;&gt; eg:<br>
&gt;&gt; let integer = Int()       // integer = 0<br>
&gt;&gt; let string    = String() // string = “”<br>
&gt;&gt; let bool      = Bool()        // bool = false<br>
&gt;&gt;<br>
&gt;&gt; That said, I’m going to -1 this proposal as well.<br>
&gt;&gt;<br>
&gt;&gt; The issue I see here is that the proposal conflates a reasonable initialization value with a “go-to default”, which is part of what Swift very deliberately did away with from Objective-C.<br>
&gt;&gt;<br>
&gt;&gt; Optional should not imply any internal value to the type. It’s the nature of them to be an internal unknown value, or nil, that must be unwrapped deliberately for the protection of your code’s logic. This seems to me to be a slippery slope to the very thing optionals are trying to avoid: default values based on the “zero / bool” conflation.<br>
&gt;&gt;<br>
&gt;&gt; Additionally, what would that pattern mean for types that cannot be initialised without parameters? If your proposal cannot support anything well beyond the most primitive types, I suspect it doesn’t scale well and shouldn’t come into the language.<br>
&gt;&gt;<br>
&gt;&gt; If you wish to use defaulting values, it’s best that you specify them instead of hoping the language specifies the one that you assume it will. Do this with the nil coalescing operator (??):<br>
&gt;&gt;<br>
&gt;&gt; print(temp ?? “”)<br>
&gt;&gt; if myString?.isEmpty ?? true {…}<br>
&gt;&gt; if myBool ?? false {…}<br>
&gt;&gt; if (myInt ?? 0) &gt; otherInt {…}<br>
&gt;&gt;<br>
&gt;&gt; This is only slightly more code, but it removes all your assumptions, and means you are now the specifier in your code’s logic. You can see from the code exactly what nil will do, and what effect it had on your code.<br>
&gt;&gt;<br>
&gt;&gt; - Rod<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt;&gt; On 11 May 2016, at 10:26 PM, Basem Emara via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; Maybe I’m missing something, but aren’t these the default values of primitives deep in the language?<br>
&gt;&gt;&gt; String = “”<br>
&gt;&gt;&gt; Int = 0<br>
&gt;&gt;&gt; Boolean = false<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; So if you needed a different default value for your code, you’d do:<br>
&gt;&gt;&gt; if !myBool?! {…} //Default to true in my app<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; You can still do which is better:<br>
&gt;&gt;&gt; if myBool ?? true {…}<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; Probably booleans is not a clear gain, but for strings it would have vast conveniences.<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; On May 11, 2016, at 8:20 AM, Patrick Smith &lt;<a href="mailto:pgwsmith@gmail.com">pgwsmith@gmail.com</a>&gt; wrote:<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt; I actually think this is less safe. It depends on the situation for what value the default should be. Sometimes it will be false, other times it will be true. So far better to explicitly show what the default is.<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; On 11 May 2016, at 10:16 PM, Basem Emara via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; Forcing unwrapping of optionals is bad practice, but safely unwrapping can be tedious. I’m hoping for something in between that would that would provide soft unwrapping using a syntax like: myVar?!<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; For example, currently we have to do this:<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; let temp = (myString ?? “”); print(“\(temp)”)<br>
&gt;&gt;&gt;&gt;&gt; if (myString ?? “”).isEmpty {…}<br>
&gt;&gt;&gt;&gt;&gt; if myBool ?? false {…}<br>
&gt;&gt;&gt;&gt;&gt; if (myInt ?? 0) &gt; otherInt {…}<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; To something like this instead:<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; print(“\(temp?!)”)<br>
&gt;&gt;&gt;&gt;&gt; if myString?!.isEmpty {…}<br>
&gt;&gt;&gt;&gt;&gt; if myBool?! {…}<br>
&gt;&gt;&gt;&gt;&gt; if myInt?! &gt; otherInt {…}<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; What this is implying is that it will attempt to unwrap or use the default of the type.<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; Of course, this will only work with primitive types and leverage their implicit default values which would go a long way with tedious code and more safety (less forced unwrapping in the world). Otherwise it will produce a compile error if doing something like: myCustomType?!. What do you think?<br>
&gt;&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;&gt;&gt; Basem<br>
&gt;&gt;&gt;&gt;&gt; _______________________________________________<br>
&gt;&gt;&gt;&gt;&gt; swift-evolution mailing list<br>
&gt;&gt;&gt;&gt;&gt; <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
&gt;&gt;&gt;&gt;&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
&gt;&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; _______________________________________________<br>
&gt;&gt;&gt; swift-evolution mailing list<br>
&gt;&gt;&gt; <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
&gt;&gt;&gt; <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
&gt;&gt;<br>
&gt;<br>
<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">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>
</div></div></blockquote></div><br></div>