<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div></div><div>Alternatively, instead of replacing the current definition with an autoclosure version, we could leave the current version in place and add a version taking a function. This could be especially useful when you’ve defined a function like `makeMySpecialKindOfButton() -> UIButton` that does a lot of customization to an object before returning it.</div><div><br></div><div>Array(repeating: { return UIView() }, count: 3)</div><div>and</div><div>Array(repeating: <span style="background-color: rgba(255, 255, 255, 0);">makeMySpecialKindOfButton, count: 3)</span></div><div><br>On Aug 17, 2017, at 7:54 AM, Haravikk via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div><meta http-equiv="Content-Type" content="text/html charset=us-ascii"><br class=""><div><blockquote type="cite" class=""><div class="">On 17 Aug 2017, at 10:38, Adrian Zubarev via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> 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"> </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 () -> 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></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>