[swift-evolution] Pitch: Soft unwrapping of optionals

Ross O'Brien narrativium+swift at gmail.com
Wed May 11 07:21:05 CDT 2016


The immediate problem I see with your suggestion is the reason why
optionals in Swift are pretty damned awesome: Swift doesn't have default
values. A Bool is not false by default; the Swift compiler will complain if
you don't initialise a Bool variable with a value, one way or the other.

The idea of combining an 'if' with a 'soft unwrap' such that if the unwrap
is nil, the 'if' fails, though - that may have merit.

On Wed, May 11, 2016 at 1: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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160511/cdcf0b78/attachment.html>


More information about the swift-evolution mailing list