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

Paul Cantrell cantrell at pobox.com
Fri Dec 18 15:50:59 CST 2015


I’d be in favor of deferring this until other discussions about the ability to return/break/continue from closures play out. With that in place, there’s even the notion that for…in could be replaced by a library function. At that point, 5.times, or repeat(5) as a library call, would look a lot more attractive.

P

> On Dec 18, 2015, at 3:41 PM, Cihat Gündüz via swift-evolution <swift-evolution at swift.org> wrote:
> 
> I agree with Radek. I find `for i in 5 { doSomething() }` or `for 5 { doSomething() }` to be very confusing since it is neither close to human language nor to any common programming language I know of.
> 
> I like the idea of giving students a step by step introduction into things, but this is IMO not the right way/place to do that.
> 
> – Cihat
> 
> 
>> Am 18.12.2015 um 22:26 schrieb Radosław Pietruszewski via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>>:
>> 
>>> 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 <mailto: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 <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 <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 <mailto:swift-evolution at swift.org>
> https://lists.swift.org/mailman/listinfo/swift-evolution <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/d6eb5f98/attachment.html>


More information about the swift-evolution mailing list