Background & details
Swift evolution proposal SE-0094 was implemented in Swift 3.0, introducing the global sequence
functions:
The latter is declared as follows
func sequence<T, State>(state: State, next: @escaping (inout State) -> T?) -> UnfoldSequence<T, State>
and is implemented in swift/stdlib/public/core/UnfoldSequence.swift. The language reference gives the following example for using it (note the lack of explicit type annotation)
// 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() })
I cannot, however, get the example above to work (e.g. using let seq1 = 1...3
, let seq2 = 4...6
), but is prompted with the rather curious error message
error: ambiguous reference to member '
sequence(first:next:)
'
Only if I explicitly type annotate the mutable State
parameter in the next
closure, as well as the return type of it, does the example above compile
let seq1 = 1...3
let seq2 = 4...6
for i in sequence(state: (false, seq1.makeIterator(), seq2.makeIterator()),
next: { (iters: inout (Bool, ClosedRangeIterator<Int>, ClosedRangeIterator<Int>))
-> Int? in
iters.0 = !iters.0
return iters.0 ? iters.1.next() : iters.2.next()
}) {
print(i)
} // 1 4 2 5 3 6
This is not the way I hope to use sequence(state:next:)
, however, as I'd rather see it in on-the-fly applications where type inference works as it should, avoiding all the explicitness.
Question
- Are we intended to use the
sequence(first:next:)
function with explicit type annotations as above? Is there some limitation in this function due to theinout
parameter closure, or am I missing something?