<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 17 Aug 2017, at 10:38, Adrian Zubarev via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="bloop_markdown" style="font-family: Helvetica, Arial; font-size: 13px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(254, 254, 254);"><p style="margin: 15px 0px; -webkit-margin-before: 0px;" class="">This is a small pitch which I will abandon if there is not much appetite for such improvement. ;)</p><p style="margin: 15px 0px;" class="">I would like to propose an improvement to an initializer of all collection types that provide:<span class="Apple-converted-space">&nbsp;</span><code style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal; -webkit-margin-before: 0px;" class="">init(repeating repeatedValue: Element, count: Int)</code>.</p><p style="margin: 15px 0px;" class="">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.</p><p style="margin: 15px 0px;" class="">The change would be really straightforward and should not break existing code, except that the behavior would change for class types.</p><p style="margin: 15px 0px;" class="">Instead of:</p><p style="margin: 15px 0px;" class=""><code style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal; -webkit-margin-before: 0px;" class="">init(repeating repeatedValue: Element, count: Int)</code></p><pre style="margin: 15px 0px; font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(204, 204, 204); overflow: auto; padding: 4px 8px; word-break: normal; word-wrap: normal;" class=""><code style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 0px; margin: 0px; padding: 0px; word-break: normal; word-wrap: normal; -webkit-margin-before: 0px;" class="">let threeViews = Array(repeating: UIView(), count: 3) // contains 1 view 3 times
</code></pre><p style="margin: 15px 0px;" class="">we would have:</p><p style="margin: 15px 0px;" class=""><code style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(234, 234, 234); margin: 0px 2px; padding: 0px 5px; word-break: normal; word-wrap: normal; -webkit-margin-before: 0px;" class="">init(repeating repeatedValue: @autoclosure () -&gt; Element, count: Int)</code></p><p style="margin: 15px 0px;" class="">This simple change would allow us to construct an array of different objects instead of an array with n references to the same object.</p><pre style="margin: 15px 0px; font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 1px solid rgb(204, 204, 204); overflow: auto; padding: 4px 8px; word-break: normal; word-wrap: normal;" class=""><code style="font-family: Menlo, Consolas, 'Liberation Mono', Courier, monospace; font-size: 10pt; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; background-color: rgb(248, 248, 248); color: inherit; border: 0px; margin: 0px; padding: 0px; word-break: normal; word-wrap: normal; -webkit-margin-before: 0px;" class="">let threeViews = Array(repeating: UIView(), count: 3) // contains 3 different view
</code></pre></div></div></blockquote></div>I don't think that changing the existing initialiser is the way to do this, as it's not really clear here that the UIView() will be instantiated multiple times rather than the same reference copied multiple times. The copying behaviour is in fact desirable as it will be significantly faster, especially with very large arrays.<div class=""><br class=""></div><div class="">I think the better way to achieve this might be add some closure-based sequence that returns a closure's result N times, so we could do something like this:</div><div class=""><br class=""></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let threeViews = Array(Repeat(count: 3) { return UIView() })</font></div><div class=""><br class=""></div><div class="">i.e- we would initialise Array from a sequence returning a new UIView three times. While we could also do an array initialiser with closure, using a separate type is probably the cleaner way to do this.</div><div class=""><br class=""></div><div class="">There is already a Repeater type, but this just does the same as the array initialiser already does, could be another type I've missed that might do what is needed, otherwise it seems to require types like AnyIterator with some counting code.</div></body></html>