[swift-evolution] A use for semi-colons: alternating between if and if let

Jordan Rose jordan_rose at apple.com
Tue Feb 2 19:13:23 CST 2016


> On Feb 2, 2016, at 16:53, Ross O'Brien via swift-evolution <swift-evolution at swift.org> wrote:
> 
> For example: getting the first element from an array of optionals. Here's my sample trivial example:
> 	var array : [Int?] = []
> 	if let x = array[0] where array.count > 0
> 	{
> 		print(x)
> 	}
> This code doesn't work. The array index is out of range. But there's no way I know of to rearrange the 'if' to ensure the array isn't empty before binding to the first element - the developer has to write a nested if statement.

We already allow this, with commas:

var array: [Int?] = []
if !array.isEmpty, let x = array[0] {
  print(x)
}

Jordan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160202/6e083617/attachment.html>


More information about the swift-evolution mailing list