<div dir="ltr"><div><div>Hi there, </div><div><br></div><div>In my opinion, `map` vs `flatMap` confusing comes from implicitly casting `T` to `T?`. </div><div>I believe it make many confusing and problems that compiler allow this implicitly casting.</div><div>1 way of solve this topic that show warning if use implicitly casting `T` to `T?`. Of course it should be explict casting can avoid this warning. Such as `x as Int?`, `Int?.some(x)`, `intClosure as (() -&gt; Int?)`.</div></div><div class="gmail_extra"><br><div class="gmail_quote">2017-10-24 7:15 GMT+09:00 Max Moiseev via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space">Hi swift-evolution!<div><br></div><div>I would like to propose the following change to the standard library:</div><div><br></div><div>deprecate `Sequence.flatMap&lt;U&gt;(_: (Element) -&gt; U?) -&gt; [U]` and make this functionality available under a new name `Sequence.filteredMap(_:)`.</div><div><br></div><div>The draft is available at <a href="https://gist.github.com/moiseev/2f36376c8ef4c2b1273cff0bfd9c3b95" target="_blank">https://gist.github.com/moi<wbr>seev/2f36376c8ef4c2b1273cff0bf<wbr>d9c3b95</a> and is included below for your convenience.</div><div><br></div><div>Max</div><div><br></div><div><h1 style="box-sizing:border-box;margin:0px 0px 16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Helvetica,Arial,sans-serif,&quot;Apple Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,&quot;Segoe UI Symbol&quot;;background-color:rgb(255,255,255)">Introduce Sequence.filteredMap(_:)</h1><ul style="box-sizing:border-box;padding-left:2em;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Helvetica,Arial,sans-serif,&quot;Apple Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,&quot;Segoe UI Symbol&quot;;font-size:16px;background-color:rgb(255,255,255)"><li style="box-sizing:border-box">Proposal: <a href="https://gist.github.com/moiseev/NNNN-filename.md" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none" target="_blank">SE-NNNN</a></li><li style="box-sizing:border-box;margin-top:0.25em">Authors: <a href="https://github.com/moiseev" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none" target="_blank">Max Moiseev</a></li><li style="box-sizing:border-box;margin-top:0.25em">Review Manager: TBD</li><li style="box-sizing:border-box;margin-top:0.25em">Status: <span style="box-sizing:border-box;font-weight:600">Awaiting implementation</span></li></ul><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Helvetica,Arial,sans-serif,&quot;Apple Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,&quot;Segoe UI Symbol&quot;;background-color:rgb(255,255,255)"><a href="https://gist.github.com/moiseev/2f36376c8ef4c2b1273cff0bfd9c3b95#introduction" class="m_557717769041133721gmail-m_-6715638371042886023anchor" id="m_557717769041133721gmail-m_-6715638371042886023user-content-introduction" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Introduction</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Helvetica,Arial,sans-serif,&quot;Apple Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,&quot;Segoe UI Symbol&quot;;font-size:16px;background-color:rgb(255,255,255)">We propose to deprecate the controversial version of a <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">Sequence.flatMap</code> method and provide the same functionality under a different, and potentially more descriptive, name.</p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Helvetica,Arial,sans-serif,&quot;Apple Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,&quot;Segoe UI Symbol&quot;;background-color:rgb(255,255,255)"><a href="https://gist.github.com/moiseev/2f36376c8ef4c2b1273cff0bfd9c3b95#motivation" class="m_557717769041133721gmail-m_-6715638371042886023anchor" id="m_557717769041133721gmail-m_-6715638371042886023user-content-motivation" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Motivation</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Helvetica,Arial,sans-serif,&quot;Apple Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,&quot;Segoe UI Symbol&quot;;font-size:16px;background-color:rgb(255,255,255)">The Swift standard library currently defines 3 distinct overloads for <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">flatMap</code>:</p><div class="m_557717769041133721gmail-m_-6715638371042886023highlight m_557717769041133721gmail-m_-6715638371042886023highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Helvetica,Arial,sans-serif,&quot;Apple Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,&quot;Segoe UI Symbol&quot;;font-size:16px;background-color:rgb(255,255,255)"><pre style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:13.600000381469727px;margin-top:0px;margin-bottom:0px;word-wrap:normal;padding:16px;overflow:auto;line-height:1.45;background-color:rgb(246,248,250);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-break:normal"><span class="m_557717769041133721gmail-m_-6715638371042886023pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">Sequence</span>.<span class="m_557717769041133721gmail-m_-6715638371042886023pl-smi" style="box-sizing:border-box">flatMap</span><span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">&lt;</span>S<span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">&gt;</span>(<span class="m_557717769041133721gmail-m_-6715638371042886023pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">_</span>: (<span class="m_557717769041133721gmail-m_-6715638371042886023pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">Element</span>) <span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">-&gt;</span> S) <span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">-&gt;</span> [S.<span class="m_557717769041133721gmail-m_-6715638371042886023pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">Element</span>]
    <span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">where</span> S <span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">:</span> <span class="m_557717769041133721gmail-m_-6715638371042886023pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">Sequence</span>
<span class="m_557717769041133721gmail-m_-6715638371042886023pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">Optional</span>.<span class="m_557717769041133721gmail-m_-6715638371042886023pl-smi" style="box-sizing:border-box">flatMap</span><span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">&lt;</span>U<span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">&gt;</span>(<span class="m_557717769041133721gmail-m_-6715638371042886023pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">_</span>: (Wrapped) <span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">-&gt;</span> U<span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">?</span>) <span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">-&gt;</span> U<span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">?</span>
<span class="m_557717769041133721gmail-m_-6715638371042886023pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">Sequence</span>.<span class="m_557717769041133721gmail-m_-6715638371042886023pl-smi" style="box-sizing:border-box">flatMap</span><span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">&lt;</span>U<span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">&gt;</span>(<span class="m_557717769041133721gmail-m_-6715638371042886023pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">_</span>: (<span class="m_557717769041133721gmail-m_-6715638371042886023pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">Element</span>) <span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">-&gt;</span> U<span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">?</span>) <span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">-&gt;</span> [U]</pre></div><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Helvetica,Arial,sans-serif,&quot;Apple Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,&quot;Segoe UI Symbol&quot;;font-size:16px;background-color:rgb(255,255,255)">The last one, despite being useful in certain situations, can be (and often is) misused. Consider the following snippet:</p><div class="m_557717769041133721gmail-m_-6715638371042886023highlight m_557717769041133721gmail-m_-6715638371042886023highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Helvetica,Arial,sans-serif,&quot;Apple Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,&quot;Segoe UI Symbol&quot;;font-size:16px;background-color:rgb(255,255,255)"><pre style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:13.600000381469727px;margin-top:0px;margin-bottom:0px;word-wrap:normal;padding:16px;overflow:auto;line-height:1.45;background-color:rgb(246,248,250);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-break:normal"><span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">struct</span> <span class="m_557717769041133721gmail-m_-6715638371042886023pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">Person</span> {
  <span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">var</span> age<span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">:</span> <span class="m_557717769041133721gmail-m_-6715638371042886023pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">Int</span>
  <span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">var</span> name<span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">:</span> <span class="m_557717769041133721gmail-m_-6715638371042886023pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">String</span>
}

<span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">func</span> <span class="m_557717769041133721gmail-m_-6715638371042886023pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">getAges</span>(<span class="m_557717769041133721gmail-m_-6715638371042886023pl-smi" style="box-sizing:border-box"><span class="m_557717769041133721gmail-m_-6715638371042886023pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">people</span></span>: [Person]) <span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">-&gt;</span> [<span class="m_557717769041133721gmail-m_-6715638371042886023pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">Int</span>] {
  <span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">return</span> people.<span class="m_557717769041133721gmail-m_-6715638371042886023pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">flatMap</span> { <span class="m_557717769041133721gmail-m_-6715638371042886023pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">$0</span>.<span class="m_557717769041133721gmail-m_-6715638371042886023pl-smi" style="box-sizing:border-box">age</span> }
}</pre></div><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Helvetica,Arial,sans-serif,&quot;Apple Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,&quot;Segoe UI Symbol&quot;;font-size:16px;background-color:rgb(255,255,255)">What happens inside <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">getNames</code> is: thanks to the implicit promotion to <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">Optional</code>, the result of the closure gets wrapped into a <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">.some</code>, then immediately unwrapped by the implementation of <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">flatMap</code>, and appended to the result array. All this unnecessary wrapping and unwrapping can be easily avoided by just using <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">map</code> instead.</p><div class="m_557717769041133721gmail-m_-6715638371042886023highlight m_557717769041133721gmail-m_-6715638371042886023highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Helvetica,Arial,sans-serif,&quot;Apple Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,&quot;Segoe UI Symbol&quot;;font-size:16px;background-color:rgb(255,255,255)"><pre style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:13.600000381469727px;margin-top:0px;margin-bottom:0px;word-wrap:normal;padding:16px;overflow:auto;line-height:1.45;background-color:rgb(246,248,250);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-break:normal"><span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">func</span> <span class="m_557717769041133721gmail-m_-6715638371042886023pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">getAges</span>(<span class="m_557717769041133721gmail-m_-6715638371042886023pl-smi" style="box-sizing:border-box"><span class="m_557717769041133721gmail-m_-6715638371042886023pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">people</span></span>: [Person]) <span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">-&gt;</span> [<span class="m_557717769041133721gmail-m_-6715638371042886023pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">Int</span>] {
  <span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">return</span> people.<span class="m_557717769041133721gmail-m_-6715638371042886023pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">map</span> { <span class="m_557717769041133721gmail-m_-6715638371042886023pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">$0</span>.<span class="m_557717769041133721gmail-m_-6715638371042886023pl-smi" style="box-sizing:border-box">age</span> }
}</pre></div><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Helvetica,Arial,sans-serif,&quot;Apple Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,&quot;Segoe UI Symbol&quot;;font-size:16px;background-color:rgb(255,255,255)">It gets even worse when we consider future code modifications, like the one where Swift 4 introduced a <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">String</code>conformance to the <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">Collection</code> protocol. The following code used to compile (due to the <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">flatMap</code> overload in question).</p><div class="m_557717769041133721gmail-m_-6715638371042886023highlight m_557717769041133721gmail-m_-6715638371042886023highlight-source-swift" style="box-sizing:border-box;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Helvetica,Arial,sans-serif,&quot;Apple Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,&quot;Segoe UI Symbol&quot;;font-size:16px;background-color:rgb(255,255,255)"><pre style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:13.600000381469727px;margin-top:0px;margin-bottom:0px;word-wrap:normal;padding:16px;overflow:auto;line-height:1.45;background-color:rgb(246,248,250);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-break:normal"><span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">func</span> <span class="m_557717769041133721gmail-m_-6715638371042886023pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">getNames</span>(<span class="m_557717769041133721gmail-m_-6715638371042886023pl-smi" style="box-sizing:border-box"><span class="m_557717769041133721gmail-m_-6715638371042886023pl-en" style="box-sizing:border-box;color:rgb(111,66,193)">people</span></span>: [Person]) <span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">-&gt;</span> [<span class="m_557717769041133721gmail-m_-6715638371042886023pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">String</span>] {
  <span class="m_557717769041133721gmail-m_-6715638371042886023pl-k" style="box-sizing:border-box;color:rgb(215,58,73)">return</span> people.<span class="m_557717769041133721gmail-m_-6715638371042886023pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">flatMap</span> { <span class="m_557717769041133721gmail-m_-6715638371042886023pl-c1" style="box-sizing:border-box;color:rgb(0,92,197)">$0</span>.<span class="m_557717769041133721gmail-m_-6715638371042886023pl-smi" style="box-sizing:border-box">name</span> }
}</pre></div><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Helvetica,Arial,sans-serif,&quot;Apple Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,&quot;Segoe UI Symbol&quot;;font-size:16px;background-color:rgb(255,255,255)">But it no longer does, because now there is a better overload that does not involve implicit promotion. In this particular case, the compiler error would be obvious, as it would point at the same line where <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">flatMap</code> is used. Imagine however if it was just a <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">let names = people.flatMap { $<a href="http://0.name" target="_blank">0.name</a> }</code> statement, and the <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">names</code> variable were used elsewhere. The compiler error would be misleading.</p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Helvetica,Arial,sans-serif,&quot;Apple Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,&quot;Segoe UI Symbol&quot;;background-color:rgb(255,255,255)"><a href="https://gist.github.com/moiseev/2f36376c8ef4c2b1273cff0bfd9c3b95#proposed-solution" class="m_557717769041133721gmail-m_-6715638371042886023anchor" id="m_557717769041133721gmail-m_-6715638371042886023user-content-proposed-solution" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Proposed solution</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Helvetica,Arial,sans-serif,&quot;Apple Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,&quot;Segoe UI Symbol&quot;;font-size:16px;background-color:rgb(255,255,255)">We propose to deprecate the controversial overload of <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">flatMap</code> and re-introduce the same functionality under a new name. The name being <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">filteredMap(_:)</code> as we believe it best describes the intent of this function.</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Helvetica,Arial,sans-serif,&quot;Apple Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,&quot;Segoe UI Symbol&quot;;font-size:16px;background-color:rgb(255,255,255)">For reference, here are the alternative names from other languages:</p><ul style="box-sizing:border-box;padding-left:2em;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Helvetica,Arial,sans-serif,&quot;Apple Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,&quot;Segoe UI Symbol&quot;;font-size:16px;background-color:rgb(255,255,255)"><li style="box-sizing:border-box">Haskell, Idris <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">
mapMaybe :: (a -&gt; Maybe b) -&gt; [a] -&gt; [b]</code></li><li style="box-sizing:border-box;margin-top:0.25em">Ocaml (Core and Batteries)
 <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">filter_map : &#39;a t -&gt; f:(&#39;a -&gt; &#39;b option) -&gt; &#39;b t</code></li><li style="box-sizing:border-box;margin-top:0.25em">F#
 <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">List.choose : (&#39;T -&gt; &#39;U option) -&gt; &#39;T list -&gt; &#39;U list</code></li><li style="box-sizing:border-box;margin-top:0.25em">Rust
 <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">fn filter_map&lt;B, F&gt;(self, f: F) -&gt; FilterMap&lt;Self, F&gt;
 where F: FnMut(Self::Item) -&gt; Option&lt;B&gt;</code></li><li style="box-sizing:border-box;margin-top:0.25em">Scala <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">
def collect[B](pf: PartialFunction[A, B]): List[B]</code></li></ul><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Helvetica,Arial,sans-serif,&quot;Apple Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,&quot;Segoe UI Symbol&quot;;background-color:rgb(255,255,255)"><a href="https://gist.github.com/moiseev/2f36376c8ef4c2b1273cff0bfd9c3b95#source-compatibility" class="m_557717769041133721gmail-m_-6715638371042886023anchor" id="m_557717769041133721gmail-m_-6715638371042886023user-content-source-compatibility" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Source compatibility</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Helvetica,Arial,sans-serif,&quot;Apple Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,&quot;Segoe UI Symbol&quot;;font-size:16px;background-color:rgb(255,255,255)">Since the old function will still be available (although deprecated) all the existing code will compile, producing a deprecation warning and a fix-it.</p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Helvetica,Arial,sans-serif,&quot;Apple Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,&quot;Segoe UI Symbol&quot;;background-color:rgb(255,255,255)"><a href="https://gist.github.com/moiseev/2f36376c8ef4c2b1273cff0bfd9c3b95#effect-on-abi-stability" class="m_557717769041133721gmail-m_-6715638371042886023anchor" id="m_557717769041133721gmail-m_-6715638371042886023user-content-effect-on-abi-stability" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Effect on ABI stability</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Helvetica,Arial,sans-serif,&quot;Apple Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,&quot;Segoe UI Symbol&quot;;font-size:16px;background-color:rgb(255,255,255)">This is an additive API change, and does not affect ABI stability.</p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Helvetica,Arial,sans-serif,&quot;Apple Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,&quot;Segoe UI Symbol&quot;;background-color:rgb(255,255,255)"><a href="https://gist.github.com/moiseev/2f36376c8ef4c2b1273cff0bfd9c3b95#effect-on-api-resilience" class="m_557717769041133721gmail-m_-6715638371042886023anchor" id="m_557717769041133721gmail-m_-6715638371042886023user-content-effect-on-api-resilience" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Effect on API resilience</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Helvetica,Arial,sans-serif,&quot;Apple Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,&quot;Segoe UI Symbol&quot;;font-size:16px;background-color:rgb(255,255,255)">Ideally, the deprecated <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">flatMap</code> overload would not exist at the time when ABI stability is declared, but in the worst case, it will be available in a deprecated form from a library post-ABI stability.</p><h2 style="box-sizing:border-box;margin-top:24px;margin-bottom:16px;line-height:1.25;padding-bottom:0.3em;border-bottom-width:1px;border-bottom-style:solid;border-bottom-color:rgb(234,236,239);color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Helvetica,Arial,sans-serif,&quot;Apple Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,&quot;Segoe UI Symbol&quot;;background-color:rgb(255,255,255)"><a href="https://gist.github.com/moiseev/2f36376c8ef4c2b1273cff0bfd9c3b95#alternatives-considered" class="m_557717769041133721gmail-m_-6715638371042886023anchor" id="m_557717769041133721gmail-m_-6715638371042886023user-content-alternatives-considered" style="box-sizing:border-box;background-color:transparent;color:rgb(3,102,214);text-decoration:none;float:left;padding-right:4px;line-height:1" target="_blank"><u></u><u></u><u></u><u></u></a>Alternatives considered</h2><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Helvetica,Arial,sans-serif,&quot;Apple Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,&quot;Segoe UI Symbol&quot;;font-size:16px;background-color:rgb(255,255,255)">It was attempted in the past to warn about this kind of misuse and do the right thing instead by means of a deprecated overload with a non-optional-returning closure. The attempt failed due to another implicit promotion (this time to <code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">Any</code>).</p><p style="box-sizing:border-box;margin-top:0px;margin-bottom:16px;color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Helvetica,Arial,sans-serif,&quot;Apple Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,&quot;Segoe UI Symbol&quot;;font-size:16px;background-color:rgb(255,255,255)">The following alternative names for this function were considered:</p><ul style="box-sizing:border-box;padding-left:2em;margin-top:0px;color:rgb(36,41,46);font-family:-apple-system,BlinkMacSystemFont,&quot;Segoe UI&quot;,Helvetica,Arial,sans-serif,&quot;Apple Color Emoji&quot;,&quot;Segoe UI Emoji&quot;,&quot;Segoe UI Symbol&quot;;font-size:16px;background-color:rgb(255,255,255);margin-bottom:0px"><li style="box-sizing:border-box"><code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">mapNonNil(_:)
</code> Does not communicate what happens to nil’s</li><li style="box-sizing:border-box;margin-top:0.25em"><code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">mapSome(_:)
</code> Reads more like «map some elements of the sequence, but not the others» rather than «process only the ones that produce an Optional.some»</li><li style="box-sizing:border-box;margin-top:0.25em"><code style="box-sizing:border-box;font-family:SFMono-Regular,Consolas,&quot;Liberation Mono&quot;,Menlo,Courier,monospace;font-size:13.600000381469727px;padding:0.2em 0px;margin:0px;background-color:rgba(27,31,35,0.0470588);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px">filterMap(_:)</code> 
Does not really follow the naming guidelines and doesn’t seem to be common enough to be considered a term of art.</li></ul><div><br></div></div></div><br>______________________________<wbr>_________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailma<wbr>n/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div></div>