<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></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 Dec 26, 2016, at 1:39 PM, Tony Allevato <<a href="mailto:tony.allevato@gmail.com" class="">tony.allevato@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" style="font-family: Helvetica; font-size: 12px; 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;" class=""><br class=""><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Mon, Dec 26, 2016 at 1:19 PM David Sweeris <<a href="mailto:davesweeris@mac.com" class="">davesweeris@mac.com</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><div class="gmail_msg" style="word-wrap: break-word;"><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">On Dec 26, 2016, at 12:10, Tony Allevato <<a href="mailto:tony.allevato@gmail.com" class="gmail_msg" target="_blank">tony.allevato@gmail.com</a>> wrote:<br class="gmail_msg"><br class="gmail_msg"></div><blockquote type="cite" class="gmail_msg"><div dir="ltr" class="gmail_msg">On Mon, Dec 26, 2016 at 11:57 AM David Sweeris via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a>> wrote:<br class="gmail_msg"><div class="gmail_quote gmail_msg"><blockquote class="gmail_quote gmail_msg" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex;"><div dir="auto" class="gmail_msg"><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">On Dec 26, 2016, at 11:35, Tony Allevato <<a href="mailto:allevato@google.com" class="gmail_msg" target="_blank">allevato@google.com</a>> wrote:<br class="gmail_msg"><br class="gmail_msg"></div><blockquote type="cite" class="gmail_msg"><div class="gmail_msg">Mathematically, identities are associated with (type, operation) pairs, not types alone.<br class="gmail_msg"><br class="gmail_msg">This conversation has put me in the column of "numeric types shouldn't have default initializers at all", personally.</div></blockquote><br class="gmail_msg"></div><div dir="auto" class="gmail_msg"><div class="gmail_msg">I'd agree, except sometimes you need a T,<span class="Apple-converted-space"> </span><i class="gmail_msg">any</i><span class="Apple-converted-space"> </span>T, for when you want to create a "pre-sized" array for stuffing results into by index:</div><div class="gmail_msg">for i in ... {</div><div class="gmail_msg"> <span class="Apple-converted-space"> </span>a[i] = ...</div><div class="gmail_msg">}</div><div class="gmail_msg">Simply saying "var a =[T](); a.reserveCapacity()" doesn't cut it because it'll still crash if you try to store anything in a[i] without somehow putting at least i+1 elements in the array first.</div></div></blockquote><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Array already has init(repeating:count:) that puts the responsibility of choosing the default value at the call site. If someone were writing a generic algorithm around this, then why not just propagate that responsibility out to its call site as well? That way, the algorithm isn't making any assumptions about what the "default" value is or even if one exists, and it doesn't impose additional requirements on the element type. For example, the user could get the default from a static factory method, an instance method on another object, or something else entirely.</div></div></div></blockquote><br class="gmail_msg"></div><div class="gmail_msg" style="word-wrap: break-word;"><div class="gmail_msg">Yeah, that's what I would use… The "filled out" example would be:</div><blockquote class="gmail_msg" style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div class="gmail_msg"><div class="gmail_msg" style="margin: 0px; line-height: normal;"><div class="gmail_msg" style="margin: 0px; line-height: normal;"><div class="gmail_msg" style="font-family: Menlo; color: rgb(187, 44, 162); margin: 0px; line-height: normal;"><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;">extension</span><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span></span><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">Array</span><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>{</span></div></div></div></div><div class="gmail_msg"><div class="gmail_msg" style="margin: 0px; line-height: normal;"><div class="gmail_msg" style="margin: 0px; line-height: normal;"><div class="gmail_msg" style="font-family: Menlo; margin: 0px; line-height: normal;"><span class="gmail_msg" style="color: rgb(187, 44, 162); font-variant-ligatures: no-common-ligatures;"> </span><span class="gmail_msg" style="color: rgb(187, 44, 162); font-variant-ligatures: no-common-ligatures;">public</span><span class="gmail_msg" style="color: rgb(187, 44, 162); font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span></span><span class="gmail_msg" style="color: rgb(187, 44, 162); font-variant-ligatures: no-common-ligatures;">func</span><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>pmap<T:<span class="Apple-converted-space"> </span></span><span class="gmail_msg" style="color: rgb(79, 129, 135); font-variant-ligatures: no-common-ligatures;">DefaultInitable</span><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;">> (transform: (</span><span class="gmail_msg" style="color: rgb(112, 61, 170); font-variant-ligatures: no-common-ligatures;">Element</span><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;">) -><span class="Apple-converted-space"> </span></span><span class="gmail_msg" style="color: rgb(79, 129, 135); font-variant-ligatures: no-common-ligatures;">T</span><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;">) -> [</span><span class="gmail_msg" style="color: rgb(79, 129, 135); font-variant-ligatures: no-common-ligatures;">T</span><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;">] {</span></div></div></div></div><div class="gmail_msg"><div class="gmail_msg" style="margin: 0px; line-height: normal;"><div class="gmail_msg" style="margin: 0px; line-height: normal;"><div class="gmail_msg" style="font-family: Menlo; margin: 0px; line-height: normal;"><span class="gmail_msg" style="color: rgb(187, 44, 162); font-variant-ligatures: no-common-ligatures;"> </span><span class="gmail_msg" style="color: rgb(187, 44, 162); font-variant-ligatures: no-common-ligatures;">var</span><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>result =<span class="Apple-converted-space"> </span></span><span class="gmail_msg" style="color: rgb(112, 61, 170); font-variant-ligatures: no-common-ligatures;">Array</span><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;"><</span><span class="gmail_msg" style="color: rgb(79, 129, 135); font-variant-ligatures: no-common-ligatures;">T</span><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;">>(repeating:</span><span class="gmail_msg" style="color: rgb(187, 44, 162); font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span></span><span class="gmail_msg" style="color: rgb(79, 129, 135); font-variant-ligatures: no-common-ligatures;">T</span><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;">(), count:<span class="Apple-converted-space"> </span></span><span class="gmail_msg" style="color: rgb(187, 44, 162); font-variant-ligatures: no-common-ligatures;">self</span><span class="gmail_msg" style="color: rgb(187, 44, 162); font-variant-ligatures: no-common-ligatures;">.</span><span class="gmail_msg" style="color: rgb(112, 61, 170); font-variant-ligatures: no-common-ligatures;">count</span><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;">)</span><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;"> </span><span class="gmail_msg" style="color: rgb(0, 132, 0); font-variant-ligatures: no-common-ligatures;">//Pick a T... any T...</span></div></div></div></div><div class="gmail_msg"><div class="gmail_msg" style="margin: 0px; line-height: normal;"><div class="gmail_msg" style="margin: 0px; line-height: normal;"><div class="gmail_msg" style="font-family: Menlo; margin: 0px; line-height: normal;"><span class="gmail_msg" style="color: rgb(187, 44, 162); font-variant-ligatures: no-common-ligatures;"> <span class="Apple-converted-space"> </span></span><span class="gmail_msg" style="color: rgb(187, 44, 162); font-variant-ligatures: no-common-ligatures;">for</span><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>i<span class="Apple-converted-space"> </span></span><span class="gmail_msg" style="color: rgb(187, 44, 162); font-variant-ligatures: no-common-ligatures;">in</span><span class="gmail_msg" style="color: rgb(187, 44, 162); font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span></span><span class="gmail_msg" style="color: rgb(187, 44, 162); font-variant-ligatures: no-common-ligatures;">self</span><span class="gmail_msg" style="color: rgb(187, 44, 162); font-variant-ligatures: no-common-ligatures;">.</span><span class="gmail_msg" style="color: rgb(112, 61, 170); font-variant-ligatures: no-common-ligatures;">indices</span><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>{</span></div></div></div></div><div class="gmail_msg"><div class="gmail_msg" style="margin: 0px; line-height: normal;"><div class="gmail_msg" style="margin: 0px; line-height: normal;"><div class="gmail_msg" style="font-family: Menlo; margin: 0px; line-height: normal;"><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;"> <span class="Apple-converted-space"> </span>result[i] =<font color="#bb2ca2" class="gmail_msg"><span class="Apple-converted-space"> </span></font></span><span class="gmail_msg" style="color: rgb(49, 89, 93); font-variant-ligatures: no-common-ligatures;">whateverTheConcurrentExectutionSyntaxIs</span><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;">(</span><span class="gmail_msg" style="color: rgb(187, 44, 162); font-variant-ligatures: no-common-ligatures;">self</span><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;">[i</span><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;">],<span class="Apple-converted-space"> </span></span><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;">transform</span><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;">)</span></div></div></div></div><div class="gmail_msg"><div class="gmail_msg" style="margin: 0px; line-height: normal;"><div class="gmail_msg" style="margin: 0px; line-height: normal;"><div class="gmail_msg" style="font-family: Menlo; margin: 0px; line-height: normal;"><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;"><font color="#bb2ca2" class="gmail_msg"> <span class="Apple-converted-space"> </span></font>}</span></div></div></div></div><div class="gmail_msg"><div class="gmail_msg" style="margin: 0px; line-height: normal;"><div class="gmail_msg" style="margin: 0px; line-height: normal;"><div class="gmail_msg" style="font-family: Menlo; margin: 0px; line-height: normal;"><span class="gmail_msg" style="color: rgb(187, 44, 162); font-variant-ligatures: no-common-ligatures;"> <span class="Apple-converted-space"> </span></span><span class="gmail_msg" style="color: rgb(187, 44, 162); font-variant-ligatures: no-common-ligatures;">return</span><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>result</span></div></div></div></div><div class="gmail_msg"><div class="gmail_msg" style="margin: 0px; line-height: normal;"><div class="gmail_msg" style="margin: 0px; line-height: normal;"><div class="gmail_msg" style="font-family: Menlo; margin: 0px; line-height: normal;"><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;"> <span class="Apple-converted-space"> </span>}</span></div></div></div></div><div class="gmail_msg"><div class="gmail_msg" style="margin: 0px; line-height: normal;"><div class="gmail_msg" style="margin: 0px; line-height: normal;"><div class="gmail_msg" style="font-family: Menlo; margin: 0px; line-height: normal;"><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;">}</span></div></div></div></div><div class="gmail_msg"><div class="gmail_msg" style="margin: 0px; line-height: normal;"><div class="gmail_msg" style="margin: 0px; line-height: normal;"><div class="gmail_msg" style="font-family: Menlo; margin: 0px; line-height: normal;"><span class="gmail_msg" style="color: rgb(187, 44, 162); font-variant-ligatures: no-common-ligatures;">var</span><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>thisCouldTakeAWhile =<span class="Apple-converted-space"> </span></span><span class="gmail_msg" style="color: rgb(112, 61, 170); font-variant-ligatures: no-common-ligatures;">Array</span><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;">((</span><span class="gmail_msg" style="color: rgb(39, 42, 216); font-variant-ligatures: no-common-ligatures;">0</span><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;">...</span><span class="gmail_msg" style="color: rgb(39, 42, 216); font-variant-ligatures: no-common-ligatures;">10000</span><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;">)).</span><span class="gmail_msg" style="color: rgb(49, 89, 93); font-variant-ligatures: no-common-ligatures;">pmap</span><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>{</span></div></div></div></div><div class="gmail_msg"><div class="gmail_msg" style="margin: 0px; line-height: normal;"><div class="gmail_msg" style="margin: 0px; line-height: normal;"><div class="gmail_msg" style="font-family: Menlo; color: rgb(49, 89, 93); margin: 0px; line-height: normal;"><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;"> <span class="Apple-converted-space"> </span></span><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;">someReallySlowFunction</span><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;">($0)</span></div></div></div></div><div class="gmail_msg"><div class="gmail_msg" style="margin: 0px; line-height: normal;"><div class="gmail_msg" style="margin: 0px; line-height: normal;"><div class="gmail_msg" style="font-family: Menlo; margin: 0px; line-height: normal;"><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;">}</span></div></div></div></div></blockquote><div class="gmail_msg"><div class="gmail_msg" style="margin: 0px; line-height: normal;"><div class="gmail_msg" style="margin: 0px; line-height: normal;"><div class="gmail_msg"><span class="gmail_msg" style="font-variant-ligatures: no-common-ligatures;">At least I<span class="Apple-converted-space"> </span><i class="gmail_msg">think</i><span class="Apple-converted-space"> </span>that’d work... I haven’t tried it yet... Anyway, without some way (<i class="gmail_msg">any</i><span class="Apple-converted-space"> </span>way) of getting an instance of T to fill in the `result` array, it becomes much trickier to keep track of all the concurrently-calculated transformed values. In this case, the semantics of `T()` are fairly irrelevant because the semantics of the<span class="Apple-converted-space"> </span><i class="gmail_msg">overall statement</i><span class="Apple-converted-space"> </span>is just to work around</span> a language limitation (Swift not having separate allocation and initialization phases), which doesn’t have anything to do with the semantics of the initial value that got passed as the `repeating` argument.</div></div></div></div></div></blockquote><div class=""><br class=""></div><div class="">This looks like it's abusing T() to stand in for nil, though. Before the result[i] assignment executes, result[i] shouldn't conceptually have a value—you're putting a default in there because the implementation requires it. T() as a placeholder is just as valid/invalid as any other value in T's space.</div></div></div></div></blockquote>Pretty much, yeah.</div><div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" style="font-family: Helvetica; font-size: 12px; 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;" class=""><div class="gmail_quote"><div class="">It's a square-peg-in-a-round-hole problem—design-wise, an array of optionals would be a better fit, but you rightly don't want to return that from the function, and you don't want to bear the cost of converting the array of optionals to an array of non-optionals after the computation is complete. That makes complete sense, but perhaps *that* is the problem that should be addressed, instead of trying to use T() to avoid it? Or alternatively, a parallel map operation should return an array of futures (or a collection type that itself is a future).</div></div></div></div></blockquote></div><div>Possibly correct… I think of it more as “solving the ’semantics’ problem from an unexpected direction”, which may well turn out to be an equivalent statement to yours (I don’t think I know enough about the topic to say for sure one way or the other). I believe adding those features is out-of-scope for now, though, so I was trying to illustrate a use-case that we might run into in the current language. Regarding using a `[T?]` and changing the last line to `return result.map {$0!}`, yeah, that’d absolutely work… it’s one more pass through the array than is <i class="">computationally</i> necessary, though, and some people need all the performance they can get. Someone mentioned `ManagedBuffer`… I’d forgotten that entire group of “*Buffer" types existed... While we’re waiting for certain features to come <i class="">in</i>-scope, it certainly sounds like one of them might be a better choice for this example.</div><div><br class=""></div><div>- Dave Sweeris</div></body></html>