[swift-evolution] guard let x = x

Martin Waitz tali at admingilde.org
Thu Nov 3 03:52:47 CDT 2016


Am 2016-11-03 09:17, schrieb Pyry Jahkola via swift-evolution:
> As pleasing as it sounds*), the idea of type narrowing breaks down
> badly if:
> 
> – the binding is implicit (without explicit extra syntax involved)
> and
> – what is bound happens to be mutable.
> 
> An example being:
> 
>  // VAR message: String?
>  IF message != NIL { // magic turns 'message' into a non-optional
> 'String' here
>  handleMessage(message)
>  message = NIL // 'String' is not 'ExpressibleByNilLiteral'
>  }
> 
> What magic would we require to still allow access to the Optional
> interface of 'message' in that block? In other words, I'm afraid type
> narrowing for Swift's enums (including Optional) isn't quite as simple
> as that.

No magic would be required.
Just don't turn into non-optional but into implicitly unwrapped 
optional.

And only do so while the compiler can prove that it is non-nil.
I.e. after your `message = nil` line, it would be handled just like any 
other optional again.

-- 
Martin


More information about the swift-evolution mailing list