[swift-evolution] [Pitch] Improve `init(repeating:count)`

Adrian Zubarev adrian.zubarev at devandartist.com
Thu Aug 17 06:09:05 CDT 2017


In terms of functionality yes potentially a breaking change, because the behavior would change for objects that were initialized through the passed expression.

Personally it always bugged me that it does not work for objects and forced map usage over a range to create an array of different objects.
However, this change feels like a fix to the intended behavior, but yes it might be very source breaking.


Yet again it’s up to the community to decide if we need this or not. ;)


Am 17. August 2017 um 12:59:06, Xiaodi Wu (xiaodi.wu at gmail.com) schrieb:

This would be very source-breaking, no?
On Thu, Aug 17, 2017 at 04:47 Jonathan Hull via swift-evolution <swift-evolution at swift.org> wrote:
+1

On Aug 17, 2017, at 2:38 AM, Adrian Zubarev via swift-evolution <swift-evolution at swift.org> wrote:

This is a small pitch which I will abandon if there is not much appetite for such improvement. ;)

I would like to propose an improvement to an initializer of all collection types that provide: init(repeating repeatedValue: Element, count: Int).

This change is meant to support reference type initialization on each iteration of the internal for-loop rather than copying the same references n times.

The change would be really straightforward and should not break existing code, except that the behavior would change for class types.

Instead of:

init(repeating repeatedValue: Element, count: Int)

let threeViews = Array(repeating: UIView(), count: 3) // contains 1 view 3 times
we would have:

init(repeating repeatedValue: @autoclosure () -> Element, count: Int)

This simple change would allow us to construct an array of different objects instead of an array with n references to the same object.

let threeViews = Array(repeating: UIView(), count: 3) // contains 3 different views




_______________________________________________
swift-evolution mailing list
swift-evolution at swift.org
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/20170817/ae71a05a/attachment.html>


More information about the swift-evolution mailing list