title: Swift @autoclosure parameter wraps provided explicit closure
body: <p>Consider the following function:</p>
  
  <pre><code>func whatever(foo: @autoclosure () -&gt; Int) {
    let x = foo()
    print(x)
  }
  </code></pre>
  
  <p>Naturally, we can invoke it like this:</p>
  
  <pre><code>whatever(foo: 5)
  // prints: 5
  </code></pre>
  
  <p>However providing an explicit closure argument causes the compiler to complain:</p>
  
  <pre><code>whatever(foo: { 5 })
  // Error: Function produces expected type 'Int'; did you mean to call it with '()'?
  </code></pre>
  
  <p>Is this the intended? Reading the documentation for <code>@autoclosure</code> I did not find a statement about whether arguments are <em>always</em> wrapped, even when providing a closure. My understanding of <code>@autoclosure</code> was:<br>
  <em>Take a closure argument. If the argument is not a closure but has the same type as the closure would return, wrap it.</em><br>
  However, the behaviour I'm seeing is: <em>Wrap the argument no matter what.</em></p>
  
  <p>A more elaborate example makes this seem very odd to me:</p>
  
  <pre><code>struct Defaults {
  
    static var dispatcher: Defaults = ...
  
    subscript&lt;T&gt;(setting: Setting&lt;T&gt;) -&gt; T { ... }
  
    struct Setting&lt;T&gt; {
      let key: String
      let defaultValue: () -&gt; T
  
      init(key: String, defaultValue: @escaping @autoclosure () -&gt; T) {
        self.key = key
        self.defaultValue = defaultValue
      }
    }
  }
  
  extension Defaults.Setting {
    static var nickname: Defaults.Setting&lt;String&gt; {
      return Defaults.Setting(key: "__nickname", defaultValue: "Angela Merkel")
    }
  }
  
  //  Usage:
  Defaults.dispatcher[.nickname] = "Emmanuel Macron"
  </code></pre>
  
  <p>Now let's say I want to hash the key of a <code>Setting</code> value:</p>
  
  <pre><code>extension Defaults.Setting {
    var withHashedKey: Defaults.Setting&lt;T&gt; {
      return Defaults.Setting(key: key.md5(), defaultValue: defaultValue)
      // Error: Cannot convert return expression of type 'Defaults.Setting&lt;() -&gt; T&gt;' to return type 'Defaults.Setting&lt;T&gt;'
    }
  }
  </code></pre>
  
  <p>To clarify: <code>defaultValue</code> is of type <code>() -&gt; T</code>. Providing it to <code>init(key: String, defaultValue: () -&gt; T)</code>, in my expectation should just work, because the argument and parameter have the same type (while parameter is <code>@autoclosure</code>).<br>However, Swift seems to wrap the provided closure, effectively creating <code>() -&gt; () -&gt; T</code>, which creates <code>Setting&lt;() -&gt; T&gt;</code> instead of <code>Setting&lt;T&gt;</code>.<br><br>
  I can work around this issue by declaring an <code>init</code> which takes an explicitly non-<code>@autoclosure</code> parameter:</p>
  
  <pre><code>extension Defaults.Setting {
    init(key: String, defaultValue: @escaping () -&gt; T) {
      self.init(key: key, defaultValue: defaultValue)
    }
  }
  </code></pre>
  
  <p>What's really daunting is that I can simply forward to the <code>init</code> taking the <code>@autoclosure</code> parameter and it works.
  <br><br>
  Am I missing something here or is it just not possible by design in Swift to provide closure arguments to <code>@autoclosure</code> parameters?</p>