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

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


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.

— 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