[swift-evolution] Proposal: An assignment operator := that ensures nil before assignment.

Ross O'Brien narrativium+swift at gmail.com
Sat Feb 27 09:02:16 CST 2016


This is less desirable than ??= would have been. To summarise the two
operators:

lhs ??= rhs. lhs is an Optional<T>, rhs is a T or Optional<T>.
If lhs is nil, assign to lhs the value of rhs. If lhs is not nil, do
nothing.

lhs := rhs. lhs is an Optional<T>, rhs is a T or Optional<T>.
If lhs is nil, assign to lhs the value of rhs. If lhs is not nil, crash
with an assertion error.

The ??= operator is safer; its behaviour is clear and it doesn't cause my
code to crash. := is more dangerous; if I want to be sure my app won't
crash, I now need to check whether lhs is nil first... and if I do that, I
might as well use the = operator.

I don't see what advantages your operator brings to Swift, or why you think
it should be accepted when ??= operator was rejected.




On Sat, Feb 27, 2016 at 2:47 PM, Amir Michail <a.michail at me.com> wrote:

>
> On Feb 27, 2016, at 9:46 AM, Ross O'Brien <narrativium+swift at gmail.com>
> wrote:
>
> How is this different from the rejected ??= proposal?
>
>
> If the value is not nil, the program stops with an assertion error.
>
> On Sat, Feb 27, 2016 at 2:34 PM, Amir Michail via swift-evolution <
> swift-evolution at swift.org> wrote:
>
>> Consider the use of “!” in the following:
>>
>> var x: Int? = 5
>>
>> x! = 2 // “!” is used to ensure non-nil value before assignment
>>
>> The idea is to have a similar method for ensuring a nil value.
>>
>> So instead of writing:
>>
>> precondition( x == nil )
>> x = 3
>>
>> You could write:
>>
>> x := 3
>>
>> _______________________________________________
>> 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/20160227/76b3d277/attachment.html>


More information about the swift-evolution mailing list