[swift-evolution] Proposal: Add .times method to Integer type
Radosław Pietruszewski
radexpl at gmail.com
Fri Dec 18 15:26:29 CST 2015
> An obvious question is, should it be equivalent to 1...n, or 0..<n?
I think that’s exactly why this isn’t a good idea. The semantics of `for i in 5` are not immediately clear at all.
If this was to be a language feature, `repeat 5`, suggested by Chris, seems like the least-ambiguous choice.
— Radek
> On 18 Dec 2015, at 22:09, Jacob Bandes-Storch via swift-evolution <swift-evolution at swift.org> wrote:
>
> This can be done pretty easily, although I think the approach has the potential to cause confusion elsewhere in code. An obvious question is, should it be equivalent to 1...n, or 0..<n?
>
> extension Int: SequenceType {
> public func generate() -> RangeGenerator<Int> {
> return (0..<self).generate()
> }
> }
>
> for i in 5 {
> print("hello \(i)")
> }
>
> Jacob Bandes-Storch
>
> On Fri, Dec 18, 2015 at 1:03 PM, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> > 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
> Architechies
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151218/e0f8ce74/attachment.html>
More information about the swift-evolution
mailing list