[swift-evolution] Extending the for loop to have multiple clauses

Chris Eidhof chris at eidhof.nl
Wed Dec 9 14:00:11 CST 2015


I think it could be really nice to extend the for-loop so that it can have multiple clauses. Much like in the if-let with multiple clauses, I could imagine a for-loop with multiple clauses:

var cards: [(Suit,Rank)] = []
for x in suits, y in ranks {
  cards.append((x,y))
}

This would be the same as writing:

var cards: [(Suit,Rank)] = []
for x in suits {
  for y in ranks {
    cards.append((x,y))}
  }
}

You could also do something like:

for x in input1, y in (x..<end) {
   // Do something with (x,y)
}

In fact, once we would have that, we could combine both if-let and for, and make it more general, to end up with something like Haskell’s do-notation or C#’s LINQ. But that might be taking it too far...

Chris


More information about the swift-evolution mailing list