[swift-evolution] Make distinction between ?? -> T and ?? -> T?
Ross O'Brien
narrativium+swift at gmail.com
Mon Mar 7 16:53:14 CST 2016
During the review of the proposal for a '??=' operator, one of the given
disadvantages was the confusion over the meaning of '??'. Since one of its
two meanings is that it nil-coalesces a chained expression into a
non-optional result, it might be misinterpreted that given the expression
'lhs ??= rhs' and a non-optional 'rhs', 'lhs' would no longer be optional.
'??' is an overloaded operator; unloading one of its two responsibilities
to a second operator would improve code clarity.
Radoslaw's example is simplistic because '[]' is unambiguously
non-optional. So let's replace it:
var value = someArray ?? someFallback ?? secondaryFallback ??
tertiaryFallback
Is 'value' optional or non-optional?
var value = someArray ?? someFallback ?? secondaryFallback ?!
tertiaryFallback
Now we can see immediately that 'value' is optional in the first case, and
non-optional in the second. However, since '!' is associated with
assertions, I agree that perhaps there's a better choice of operator for
this meaning.
On Mon, Mar 7, 2016 at 10:26 PM, Radosław Pietruszewski <
swift-evolution at swift.org> wrote:
> -1, never seen a real world situation where this would be
> confusing/problematic.
>
> Having one operator is nice for chaining:
>
> var value = someArray ?? someFallback ?? secondaryFallback ?? []
>
> — Radek
>
> On 07 Mar 2016, at 22:29, Sébastien Blondiau via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> Currently, there is only one nil coalescing operator, which may return an
> optional value or not, depending on the second parameter. This variability
> can leads to unclear line of code:
>
> var isItOptionalOrNot = value ?? otherValue
>
> I think there should be two distinct operators:
>
> var certainlyOptional = value ?? otherValue
> var certainlyNotOptional = value ?! notOptionalValue
>
> In my point of view, this differentiation brings more clarity about wether
> the result is an optional or not.
>
> What's your opinion?
>
> --
> Sébastien Blondiau
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
>
> _______________________________________________
> 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/20160307/e9689bd2/attachment.html>
More information about the swift-evolution
mailing list