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

James Hillhouse IV jimhillhouse at me.com
Thu Feb 4 18:04:19 CST 2016


Here’s a question that Drew raised as he was educating me on this discussion. Given that the following works, are you proposing to deprecate,

if let a = 2, var b = 3, let c = 4, var d = 5{ } // 1

in favor of?

if let a = 2; var b = 3; let c = 4; var d = 5{ } // 2

If semi-colon is still being considered, can Joe or Jordon clarify?

> On Feb 2, 2016, at 7:40 PM, Joe Groff via swift-evolution <swift-evolution at swift.org> wrote:
> 
>> 
>> On Feb 2, 2016, at 5:13 PM, Jordan Rose via swift-evolution <swift-evolution at swift.org <mailto: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.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160204/a7610ece/attachment.html>


More information about the swift-evolution mailing list