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

Joe Groff jgroff at apple.com
Tue Feb 2 19:40:29 CST 2016


> On Feb 2, 2016, at 5:13 PM, Jordan Rose via swift-evolution <swift-evolution at swift.org> wrote:
> 
> 
>> On Feb 2, 2016, at 16:53, Ross O'Brien via swift-evolution <swift-evolution at swift.org <mailto: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)
> }

That's true, but there are some problematic ambiguities with our current syntax, since 'let' and 'case' conditions both admit multiple sub-conditions also separated by commas, e.g. 'if let x = y, y = z { }' or 'if case let x = y, let y = z'. Using ';' for this purpose would be less ambiguous.

-Joe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160202/9551fc2f/attachment.html>


More information about the swift-evolution mailing list