<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body text="#000000" bgcolor="#FFFFFF">
    <div class="moz-cite-prefix">On 21.10.17 02:50, Santiago Gil via
      swift-users wrote:<br>
    </div>
    <blockquote type="cite"
cite="mid:CACXZkwa9zKWf3ELwXsbpLYF8DGUxFdAHstzrkq=nVHPD4pgbbQ@mail.gmail.com">
      <div dir="ltr">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>
      </div>
    </blockquote>
    <br>
    It looks like the compiler infers the type of the closure in the
    second example to be <tt>(String) -&gt; Optional&lt;String&gt;</tt>
    instead of <tt>String -&gt; String</tt>.<br>
    <br>
    I don't know why and I definitely find it surprising.<br>
  </body>
</html>