[swift-evolution] Proposal: Remove the underscore and `in` for `for` loop

Charlie Monroe charlie at charliemonroe.net
Tue Jul 5 12:47:51 CDT 2016


Sorry, my example should have been the other way around:

var data: NSData!
for 1...5 {
	data = self.loadSomeData() 
	if data != nil {
		break // *this* can't be done with .forEach
	}
	
	/// Try again in next iteration
}

if data == nil {
	return
}

/// Process data.

But yeah, it can be written the other way around to avoid break.

> On Jul 5, 2016, at 6:13 PM, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
> 
> `continue` can be rewritten `return` inside `forEach`.
> On Tue, Jul 5, 2016 at 02:30 Charlie Monroe via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> Example:
> 
> for 1...5 {
> 	guard let data = self.loadSomeData() else {
> 		/// Will try it several times and then report failure to the user.
> 		continue
> 	}
> 	
> 	// process data
> 	return
> }
> 
> // Error, failed to load data even after retrying.
> 
>> On Jul 5, 2016, at 9:23 AM, Jose Cheyo Jimenez <cheyo at masters3d.com <mailto:cheyo at masters3d.com>> wrote:
>> 
>> How would you build a condition to break if you are ignoring each value ? Unless you are hard coding a condition in which case I would still argue that the proposed shorthand for is less clear than `for _ in` or forEach. 
>> 
>> On Jul 4, 2016, at 9:29 PM, Charlie Monroe <charlie at charliemonroe.net <mailto:charlie at charliemonroe.net>> wrote:
>> 
>>>> -1 this is why we have collection.forEach{}
>>>> 
>>>> (1...10).forEach {
>>>> // do something. 
>>>> }
>>> 
>>> This is not equivalent since it doesn't allow you to break from the for loop.
>>> 
>>>> 
>>>> On Jul 1, 2016, at 12:38 AM, Diego Barros via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>>> 
>>>>> When you want a simple `for` loop, for example:
>>>>> 
>>>>> for _ in 1...10 {
>>>>> 
>>>>> // do something 10 times
>>>>> 
>>>>> }
>>>>> 
>>>>> 
>>>>> 
>>>>> Clean-up and simplify the syntax by removing the superfluous underscore and `in`:
>>>>> 
>>>>> 
>>>>> 
>>>>> for 1...10 {
>>>>> 
>>>>> // do something 10 times
>>>>> 
>>>>> 
>>>>> }
>>>>> 
>>>>> 
>>>>> -- diego
>>>>> _______________________________________________
>>>>> swift-evolution mailing list
>>>>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>>>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>>>> _______________________________________________
>>>> swift-evolution mailing list
>>>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>>>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>>> 
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>

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


More information about the swift-evolution mailing list