[swift-evolution] SE-0105: Removing Where Clauses from For-In Loops

Karl razielim at gmail.com
Mon Jun 27 21:51:56 CDT 2016


> On 24 Jun 2016, at 17:10, Charlie Monroe via swift-evolution <swift-evolution at swift.org> wrote:
> 
> If the `where` keyword were to stay in the language, how would you feel about extending it? One major argument is that it is not as powerful as guard or if. How about something like this was allowed:
> 
> for text in self.texts where let data = text.data(usingEncoding: NSASCIIEncoding) {
> 	/// Do something with data or text
> }
> 

I noticed this in some code I was writing just now and came to mention it.

Right now I’ve got this (Note: tagsForGroup is [String?]):

> for (index, range) in zip(groupRanges.indices, groupRanges) where tagsForGroup[index] != nil {
> 
> 	let tagForGroup = tagsForGroup[index]!
> 	// tag `range` with `tagForGroup`
> }

It would be nicer to write it like this:

> for (index, range) in zip(groupRanges.indices, groupRanges) where let tag = tagsForGroup[index] {
> 
> 	// tag `range` with `tag`
> }


I could do it with a guard, but as somebody so eloquently put, it produces more divergent constructs for readers. It’s part of a complex regex matching and processing workflow, so I want to keep it readable and make it explicit that we’re only tagging/processing groups where a tag is defined.


Karl



More information about the swift-evolution mailing list