[swift-evolution] [Pitch] Improve `init(repeating:count)`
Xiaodi Wu
xiaodi.wu at gmail.com
Thu Aug 17 20:38:00 CDT 2017
On Thu, Aug 17, 2017 at 8:25 PM, Erica Sadun <erica at ericasadun.com> wrote:
> `repeatElement((), count: 5)` is better than `1 ... 5`, but
> `Count(3).map({ UIView() })` is far more elegant. I'd still probably go
> with an array initializer or `5.elements(of: UIView())`. I don't think I'm
> overstating how common this pattern is, and `Array(repeating:count:)` feels
> _close_ but not close enough.
>
The first two have the benefit of being currently existing APIs; they
capture the semantics perfectly, as I've argued, and I simply don't see how
they are impaired in elegance in any way--certainly not enough to justify a
standard library addition to create a third way of spelling the same thing.
Ultimately, any user is free to define something like:
```
func * <T>(lhs: Int, rhs: @autoclosure () throws -> T) rethrows -> [T] {
return try repeatElement((), count: lhs).map { try rhs() }
}
5 * UIView()
```
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170817/713e2d46/attachment.html>
More information about the swift-evolution
mailing list