title: Compiler cannot infer return type
body: <p>Swift is unable to infer the return type of a function, whose parameter is a closure, and where the return type is deduced from the closure´s return type.</p>
  
  <p>Given this generic class:</p>
  
  <pre><code>class Bar&lt;T&gt; {
      init(_ v:T) {}
  
      func wrap&lt;R&gt;(f:()-&gt;R) -&gt; Bar&lt;R&gt; {
          return Bar&lt;R&gt;(f())
      }
      func wrap(f:()-&gt;()) -&gt; () {
          f()
      }
  }
  </code></pre>
  
  <p>The function <code>wrap</code> is overloaded, once taking a Closure as parameter which returns <code>()</code>, and another time taking a Closure as parameter which returns a type specified as a type parameter (<code>R</code>) for the function.</p>
  
  <p>In the following snippet, it seems obvious that the return type of the Closure passed as an argument to the function <code>wrap</code> is a <code>String</code>:</p>
  
  <pre><code>var bar :Bar&lt;Int&gt; = Bar&lt;Int&gt;(0)
  
  let b0 = bar.wrap {
      return ""
  }
  </code></pre>
  
  <p>IMHO, the compiler should now be able to deduce the return type of <code>wrap</code> as type <code>Bar&lt;String&gt;</code>, and thus should be able to infer the type of the constant <code>b0</code> accordingly.</p>
  
  <p>The compiler (as of beta 3) issues the following errors:</p>
  
  <pre><code>main.swift:53:16: error: type '()' does not conform to protocol 'StringLiteralConvertible'
      return ""
             ^
  main.swift:51:9: warning: constant 'b0' inferred to have type '()', which may be unexpected
  let b0 = bar.wrap {
      ^
  main.swift:51:9: note: add an explicit type annotation to silence this warning
  let b0 = bar.wrap {
      ^
        : ()
  </code></pre>
  
  <p>Either of this workaround would fix the issue:</p>
  
  <pre><code>let b1 = bar.wrap {
      () -&gt; String in
      return ""
  }
  let b2:Bar&lt;String&gt; = bar.wrap {
      return ""
  }
  </code></pre>
  
  <p><strong>Edit:</strong>  added Antonio's answer as another version which compiles correctly:</p>
  
  <pre><code>let b0 = bar.wrap { "" }
  </code></pre>
  
  <p><strong>The question is:</strong></p>
  
  <p>Is there a compelling reason for the compiler not being able to infer the return type in the failing examples, assuming it is working as specified?</p>
  
  <p>Again, please consider this:</p>
  
  <pre><code>works: let b0 = bar.wrap { 0 }
  fails: let b0 = bar.wrap { return 0 }
  </code></pre>
  
  <p>Or, is this possibly the effect of an incomplete or incorrect implementation of a beta status compiler?</p>
  
  <p>Note: Please, do not state your opinions here, only clear facts - if any. ;)  </p>