[swift-evolution] Pitch: Soft unwrapping of optionals
Basem Emara
contact at basememara.com
Wed May 11 07:26:25 CDT 2016
Maybe I’m missing something, but aren’t these the default values of primitives deep in the language?
String = “”
Int = 0
Boolean = false
So if you needed a different default value for your code, you’d do:
if !myBool?! {…} //Default to true in my app
You can still do which is better:
if myBool ?? true {…}
Probably booleans is not a clear gain, but for strings it would have vast conveniences.
> On May 11, 2016, at 8:20 AM, Patrick Smith <pgwsmith at gmail.com> wrote:
>
> 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.
>
>
>> On 11 May 2016, at 10:16 PM, Basem Emara via swift-evolution <swift-evolution at swift.org> wrote:
>>
>> 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?!
>>
>> For example, currently we have to do this:
>>
>> let temp = (myString ?? “”); print(“\(temp)”)
>> if (myString ?? “”).isEmpty {…}
>> if myBool ?? false {…}
>> if (myInt ?? 0) > otherInt {…}
>>
>> To something like this instead:
>>
>> print(“\(temp?!)”)
>> if myString?!.isEmpty {…}
>> if myBool?! {…}
>> if myInt?! > otherInt {…}
>>
>> What this is implying is that it will attempt to unwrap or use the default of the type.
>>
>> 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?
>>
>> Basem
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>
More information about the swift-evolution
mailing list