[swift-evolution] Proposal: Add .times method to Integer type

Brent Royal-Gordon brent at architechies.com
Fri Dec 18 15:03:41 CST 2015

> I’d like to propose an addition of a useful method, especially for beginners that also makes Swift much more readable in some situations: The addition of a .times method to Integer type(s).

I’ve said it before, but I don’t think `times` is a good solution for learners. It teaches them a form of looping they will never use in practice and does not allow them to practice with ancillary loop skills like `break` and `continue`.

I think our best bet is to extend the `for` loop to allow a single number, meaning either `1…n` or `0..<n` (you can argue it either way), and also to allow the `variableName in` part to be omitted, meaning `_ in`. This gives us the pedagogical simplicity of a “do this N times” loop, but couches it in a form where, when the student moves on, more commonly used loop forms are a straightforward extension of that simple case.

	for 5 { print(“Hello!”) }
	for i in 5 { print(“Hello \(i)!”) }
	for i in 10..<20 { print(“Hello \(i)!”) }
	for i in 10...20 { print(“Hello \(i)!”) }

Brent Royal-Gordon

More information about the swift-evolution mailing list