[swift-evolution] [Review] SE-0007 Remove C-style for-loops with conditions and incrementers

Michel Fortin michel.fortin at michelf.ca
Thu Dec 10 14:44:12 CST 2015

Le 10 déc. 2015 à 15:13, Stephen Celis <stephen.celis at gmail.com> a écrit :

> Can't you do the following?
>     var i = 10, j = 20; while i > 0 && j > 0 { defer { i -= 1; j -= 2 }
>         // ...
>     }

In this particular case you can and it won't have any noticeable side effects. But in the general case it has a different behaviour: the increment part is called on break or throw. Just imagine a loop body like this one:

	defer { i += 1 }
	if i == Int.max { break }

I know I keep coming up with made-up examples to disqualify smart replacements. That might seem artificial, and it certainly is. But any migration tool needs to guaranty identical behaviour.

I'm also starting to wonder if this example with defer that keeps coming up (it's probably the third time already) is an indicator that people will start to depend on clever tricks like this one, failing to realize the subtle change in semantics it introduces.

Michel Fortin
michel.fortin at michelf.ca

More information about the swift-evolution mailing list