<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'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"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></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>
> On 11 May 2016, at 11:17 PM, Basem Emara <<a href="mailto:contact@basememara.com">contact@basememara.com</a>> wrote:<br>
><br>
> Thanks for the input everybody, and for the deeper analysis, Rod.<br>
><br>
> 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>
><br>
> Happy coding! -Basem<br>
><br>
>> On May 11, 2016, at 9:10 AM, Rod Brown <<a href="mailto:rodney.brown6@icloud.com">rodney.brown6@icloud.com</a>> wrote:<br>
>><br>
>> I think what you’re referring to as default values would be what you get if you initialize the type directly.<br>
>><br>
>> eg:<br>
>> let integer = Int() // integer = 0<br>
>> let string = String() // string = “”<br>
>> let bool = Bool() // bool = false<br>
>><br>
>> That said, I’m going to -1 this proposal as well.<br>
>><br>
>> 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>
>><br>
>> 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>
>><br>
>> 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>
>><br>
>> 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>
>><br>
>> print(temp ?? “”)<br>
>> if myString?.isEmpty ?? true {…}<br>
>> if myBool ?? false {…}<br>
>> if (myInt ?? 0) > otherInt {…}<br>
>><br>
>> 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>
>><br>
>> - Rod<br>
>><br>
>><br>
>><br>
>>> On 11 May 2016, at 10:26 PM, Basem Emara via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br>
>>><br>
>>> Maybe I’m missing something, but aren’t these the default values of primitives deep in the language?<br>
>>> String = “”<br>
>>> Int = 0<br>
>>> Boolean = false<br>
>>><br>
>>> So if you needed a different default value for your code, you’d do:<br>
>>> if !myBool?! {…} //Default to true in my app<br>
>>><br>
>>> You can still do which is better:<br>
>>> if myBool ?? true {…}<br>
>>><br>
>>> Probably booleans is not a clear gain, but for strings it would have vast conveniences.<br>
>>><br>
>>>> On May 11, 2016, at 8:20 AM, Patrick Smith <<a href="mailto:pgwsmith@gmail.com">pgwsmith@gmail.com</a>> wrote:<br>
>>>><br>
>>>> 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>
>>>><br>
>>>><br>
>>>>> On 11 May 2016, at 10:16 PM, Basem Emara via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br>
>>>>><br>
>>>>> 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>
>>>>><br>
>>>>> For example, currently we have to do this:<br>
>>>>><br>
>>>>> let temp = (myString ?? “”); print(“\(temp)”)<br>
>>>>> if (myString ?? “”).isEmpty {…}<br>
>>>>> if myBool ?? false {…}<br>
>>>>> if (myInt ?? 0) > otherInt {…}<br>
>>>>><br>
>>>>> To something like this instead:<br>
>>>>><br>
>>>>> print(“\(temp?!)”)<br>
>>>>> if myString?!.isEmpty {…}<br>
>>>>> if myBool?! {…}<br>
>>>>> if myInt?! > otherInt {…}<br>
>>>>><br>
>>>>> What this is implying is that it will attempt to unwrap or use the default of the type.<br>
>>>>><br>
>>>>> 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>
>>>>><br>
>>>>> Basem<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>
>>>><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>
>><br>
><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>