title: Global function sequence(state:next:) and type inference
body: <h3>Background &amp; details</h3>
  <p>Swift evolution proposal <a href="https://github.com/apple/swift-evolution/blob/master/proposals/0094-sequence-function.md" rel="nofollow noreferrer">SE-0094</a> was implemented in Swift 3.0, introducing the global <code>sequence</code> functions:</p>
  <ul>
  <li><a href="https://developer.apple.com/reference/swift/2015879-sequence" rel="nofollow noreferrer"><code>sequence(first:next:)</code></a></li>
  <li><a href="https://developer.apple.com/reference/swift/2011998-sequence" rel="nofollow noreferrer"><code>sequence(state:next:)</code></a></li>
  </ul>
  <p>The latter is declared as follows</p>
  <blockquote>
  <pre><code>func sequence&lt;T, State&gt;(state: State, 
                          next: @escaping (inout State) -&gt; T?) -&gt;
           UnfoldSequence&lt;T, State&gt;
  </code></pre>
  </blockquote>
  <p>and is implemented in <a href="https://github.com/apple/swift/blob/master/stdlib/public/core/UnfoldSequence.swift" rel="nofollow noreferrer">swift/stdlib/public/core/UnfoldSequence.swift</a>. The language reference gives the following example for using it (note the lack of explicit type annotation)</p>
  <blockquote>
  <pre><code>// Interleave two sequences that yield the same element type
  sequence(state: (false, seq1.makeIterator(), seq2.makeIterator()), next: { iters in
    iters.0 = !iters.0
    return iters.0 ? iters.1.next() : iters.2.next()
  })
  </code></pre>
  </blockquote>
  <p>I cannot, however, get the example above to work (e.g. using <code>let seq1 = 1...3</code>, <code>let seq2 = 4...6</code>), but is prompted with the rather curious error message</p>
  <blockquote>
  <p>error: ambiguous reference to member '<code>sequence(first:next:)</code>'</p>
  </blockquote>
  <p>Only if I explicitly type annotate the mutable <code>State</code> parameter in the <code>next</code> closure, as well as the return type of it, does the example above compile</p>
  <pre><code>let seq1 = 1...3
  let seq2 = 4...6
  
  for i in sequence(state: (false, seq1.makeIterator(), seq2.makeIterator()), 
          next: { (iters: inout (Bool, ClosedRangeIterator&lt;Int&gt;, ClosedRangeIterator&lt;Int&gt;)) 
                          -&gt; Int? in
      iters.0 = !iters.0
      return iters.0 ? iters.1.next() : iters.2.next()
  }) {
      print(i)
  } // 1 4 2 5 3 6
  </code></pre>
  <p>This is not the way I hope to use <code>sequence(state:next:)</code>, however, as I'd rather see it in on-the-fly applications where type inference works as it should, avoiding all the explicitness.</p>
  <h3>Question</h3>
  <ul>
  <li>Are we intended to use the <code>sequence(first:next:)</code> function with explicit type annotations as above? Is there some limitation in this function due to the <code>inout</code> parameter closure, or am I missing something?</li>
  </ul>