<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Aug 17, 2017 at 9:06 PM, Erica Sadun via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;"><br><div><span class="gmail-"><blockquote type="cite"><div>On Aug 17, 2017, at 6:56 PM, Xiaodi Wu &lt;<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.com</a>&gt; wrote:</div><br class="gmail-m_-4653050338675132092Apple-interchange-newline"><div><div dir="ltr">On Thu, Aug 17, 2017 at 7:51 PM, Erica Sadun <span dir="ltr">&lt;<a href="mailto:erica@ericasadun.com" target="_blank">erica@ericasadun.com</a>&gt;</span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;"><div>What people are doing is taking a real set of values (1, 2, 3, 4, 5, for example), then discarding them via `_ in`, which is different from `Void -&gt; T` or `f(x) = 0 * x`. The domain could just as easily be (Foo(), &quot;b&quot;, đź’©, Â UIColor.red, { x: Int in x^x }). There are too many semantic shifts away from &quot;I would like to collect the execution of this closure n times&quot; for it to sit comfortably.</div></div></blockquote><div><br></div><div>What arguments might help to alleviate this discomfort? Clearly, functions exist that can map this delightfully heterogeneous domain to some sort of range that the user wants. Would you feel better if we wrote instead the following?</div><div><br></div><div>```</div><div>repeatElement((), count: 5).map { UIView() }</div><div>```</div></div></div></div></div></blockquote><div><br></div></span>My favorite solution is the array initializer. Something along the lines of `Array&lt;T&gt;(count n: Int, generator: () -&gt; T)`. I&#39;m not sure it _quite_ reaches standard library but I think it is a solid way to say &quot;produce a collection with a generator run n times&quot;. It&#39;s a common Â task. I was asking around about this, and found that a lot of us who work with both macOS and iOS and want to stress test interfaces do this very often. Other use cases include &quot;give me n random numbers&quot;, &quot;give me n records from this database&quot;, etc. along similar lines.</div><div><br></div><div>The difference between this and the current `Array(repeating:count:)` initializer is switching the arguments and using a trailing closure Â (or an autoclosure) rather than a set value. That API was designed without the possibility that you might want to repeat a generator, so there&#39;s a bit of linguistic turbulence.</div><span class="gmail-"><div><br></div><div>-- E</div></span><br></div></blockquote></div><br></div><div class="gmail_extra">To me at least, this is a very i-Centric complaint, since I can barely remember the last time I needed something like this for anything that didn’t involve UIKit. What you’re asking for is API sugar for generating reference types with less typing. The problem is it’s hugely disruptive to user’s mental model of how Swift operates, to say the least. When I read a function signature like Array.init(count:object:) or whatever, I expect that if a referency type is passed to the object parameter, the <i>pointer</i> gets copied `<span style="font-family:monospace,monospace">count</span>` times, and the reference count incremented accordingly. I don’t expect the object itself to get deepcopied. That’s why the use of @autoclosure is strongly <a href="https://www.google.com/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=5&amp;cad=rja&amp;uact=8&amp;ved=0ahUKEwjar8qe6d_VAhUC4YMKHcIIC5MQygQIRDAE&amp;url=https%3A%2F%2Fdeveloper.apple.com%2Flibrary%2Fcontent%2Fdocumentation%2FSwift%2FConceptual%2FSwift_Programming_Language%2FClosures.html%23%2F%2Fapple_ref%2Fdoc%2Fuid%2FTP40014097-CH11-ID543&amp;usg=AFQjCNHnH4tzGagCKcjU5jRzK-GSDCB6qA">recommended against</a>, and why it only appears in things relating to debugging like <span style="font-family:monospace,monospace">assert(condition:message:file:line:)</span>, which are mentally separated from the rest of Swift.<br><br></div><div class="gmail_extra">Doing this with a function is less disruptive, but if you ask me, it expands API surface area without offering any real benefit. As people have pointed out, we already have a perfectly good idiom in <span style="font-family:monospace,monospace">(0 ..&lt; n).map{ _ in UIView() }</span>. I never understood why certain people complain about â€śdiscarding the domain”, but have no problem writing things like this:<br><br></div><div class="gmail_extra"><span style="font-family:monospace,monospace">for _ in 0 ..&lt; n <br>{ <br>    //<br>}<br><br></span></div><div class="gmail_extra"><span style="font-family:arial,helvetica,sans-serif">If you’re truly allergic to </span><span style="font-family:monospace,monospace">map{ _ in UIView() }</span><span style="font-family:arial,helvetica,sans-serif">, might</span><span style="font-family:monospace,monospace"><font face="arial,helvetica,sans-serif"> I suggest the following:<br><br></font></span></div><div class="gmail_extra"><span style="font-family:monospace,monospace">var array:[UIView] = []<br></span></div><div class="gmail_extra"><span style="font-family:monospace,monospace">    array.reserveCapacity(n)<br></span></div><div class="gmail_extra"><span style="font-family:monospace,monospace">for _ in 0 ..&lt; n <br>{<br></span></div><div class="gmail_extra"><span style="font-family:monospace,monospace">    { array.append(UIView()) }()<br>}<font face="arial,helvetica,sans-serif"><br><br></font></span></div><div class="gmail_extra"><span style="font-family:monospace,monospace"><font face="arial,helvetica,sans-serif">That’s really all an array initializer that takes a block is really doing. Or just use </font>map<font face="arial,helvetica,sans-serif">.<br></font></span></div></div>