<div dir="ltr">Hi Swift users,<br><br>I just ran into a bug after a Swift 4 migration where we expected [String] and got [Character] from a flatMap since flatMap will flatten the String since it's now a collection. While I understand why flatMap behaves this way now that string are collections, in testing this I found some weird behaviour...<br><br>
<p class="inbox-inbox-p1"><font face="monospace"><span class="inbox-inbox-s1" style="">var</span> strArr = [<span class="inbox-inbox-s2" style="">"Hi"</span>, <span class="inbox-inbox-s2" style="">"hello"</span>]</font></p>
<p class="inbox-inbox-p1"><font face="monospace"><span class="inbox-inbox-s1">let</span> result = <span class="inbox-inbox-s3">strArr</span>.<span class="inbox-inbox-s4">flatMap</span> { x <span class="inbox-inbox-s1">in</span></font></p>
<p class="inbox-inbox-p4"><font face="monospace"><span class="inbox-inbox-s5"><span class="inbox-inbox-Apple-converted-space"> </span></span>return<span class="inbox-inbox-s5"> x</span></font></p>
<p class="inbox-inbox-p1"><font face="monospace">}</font></p><p class="inbox-inbox-p2"><br>The type of results ends up being <font face="monospace">[Character] </font>in the above case. However, adding a print statement changes things.<br><br></p><p class="inbox-inbox-inbox-inbox-p1"><font face="monospace"><span class="inbox-inbox-inbox-inbox-s1">var</span> strArr = [<span class="inbox-inbox-inbox-inbox-s2">"Hi"</span>, <span class="inbox-inbox-inbox-inbox-s2">"hello"</span>]</font></p><p class="inbox-inbox-inbox-inbox-p1"><font face="monospace"><span class="inbox-inbox-inbox-inbox-s1">let</span> result = <span class="inbox-inbox-inbox-inbox-s3">strArr</span>.<span class="inbox-inbox-inbox-inbox-s4">flatMap</span> { x <span class="inbox-inbox-inbox-inbox-s1">in</span></font></p><p class="inbox-inbox-inbox-inbox-p1"><span class="inbox-inbox-inbox-inbox-Apple-converted-space" style="font-family:monospace"> </span><span style="font-family:monospace">print(x)</span></p><p class="inbox-inbox-inbox-inbox-p4"><font face="monospace"><span class="inbox-inbox-inbox-inbox-s5"><span class="inbox-inbox-inbox-inbox-Apple-converted-space"> </span></span>return<span class="inbox-inbox-inbox-inbox-s5"> x</span></font></p><p class="inbox-inbox-inbox-inbox-p1"><font face="monospace">}</font></p><p class="inbox-inbox-inbox-inbox-p2">In this case, result is of type <font face="monospace">[String]</font><br><br>This seems like a bug, or is this expected Swift behaviour?</p><p class="inbox-inbox-inbox-inbox-p2">Thanks,<br><br>Santiago</p></div>