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

Radosław Pietruszewski radexpl at gmail.com
Sat Feb 27 11:03:11 CST 2016


Apologies — I misunderstood.

In this case, like others, I would like to see a compelling snippet of code showing the use case for this. Without this context, I just don’t see why would you want to do it this way.

— Radek

> On 27 Feb 2016, at 17:59, Amir Michail <a.michail at me.com> wrote:
> 
> 
>> On Feb 27, 2016, at 11:32 AM, Radosław Pietruszewski <radexpl at gmail.com> wrote:
>> 
>> Expressing the constraint of “can’t be nil” is much better done via the type system, where the undesirable value is simply impossible. Instead of inducing crashes with innocently-looking special operator, just use a non-optional variable, and where you have to convert from an optional, unwrap it explicitly.
>> 
> 
> The constraint actually is “has to be nil”.
> 
>> — Radek
>> 
>>> On 27 Feb 2016, at 15:34, 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
>> 
> 



More information about the swift-evolution mailing list