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

Amir Michail a.michail at me.com
Sat Feb 27 09:06:01 CST 2016


> On Feb 27, 2016, at 10:02 AM, Ross O'Brien <narrativium+swift at gmail.com> wrote:
> 
> 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.

This is how I write code. I use preconditions everywhere because I want to know about errors as soon as possible.

> 
> 
> 
> 
> On Sat, Feb 27, 2016 at 2:47 PM, Amir Michail <a.michail at me.com <mailto:a.michail at me.com>> wrote:
> 
>> On Feb 27, 2016, at 9:46 AM, Ross O'Brien <narrativium+swift at gmail.com <mailto: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 <mailto: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 <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution <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/3d6f9109/attachment.html>


More information about the swift-evolution mailing list