[swift-evolution] A (better) Swift Equivalent For The Classical For-Loop With Numeric Scalars

Brent Royal-Gordon brent at architechies.com
Sun Mar 20 14:27:43 CDT 2016


> A simple example might be:   for (altitude = 0.1; altitude < 10e6; altitude =* 10.0) { . . . }

Conceptually, this could be represented as a variant of `stride` which took a unary function for its `by` parameter:

	for altitude in stride(from: 0.1, to: 10e6, by: { $0 * 10 }) {
		...
	}

There are a couple of features which could in theory clean this syntax up a bit, but they have limitations that prevent it:

* You can't use trailing closure syntax in a statement like `for` or `if`; the trailing closure is treated as the body of the statement.
* You can't use @autoclosure for an expression which has a parameter.

-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list