[swift-evolution] Passing an optional first argument to sequence(first:next:)

Braeden Profile jhaezhyr12 at gmail.com
Tue Aug 16 19:34:08 CDT 2016

Okay, I’m actually confused about the current state of things.

Earlier, this was introduced:

// This function is silly,
// but useful functions may take a similar form
func foo(x: Int?) -> Int? {
  guard let x = x else { return 0 }
  return x > 12 ? nil : x + 1

let a: Int? = nil
for i in sequence(first: a, next: { foo($0) })
  // This is a pretty useless thing to do,
  // but there are useful things that can be done
  // without checking whether `i == nil`

…and it returns 14 lines of output.  But this doesn’t make sense.  I expected the anonymous closure for next in sequence(first: Int?, next: (Int?) -> Int??) to raise (foo($0)) to (Optional(foo($0))) to avoid having the type signature end up being (Int?) -> Int?.  In that case, the result of (foo($0)) would always be encapsulated in Optional.Some(), so at the 15th run, it would return (Optional.Some(nil)).  Yet it stops when foo returns nil.  Why is this?

And if you replace "next: { foo($0) }" with "next: foo", then it compiles with the same result.  Replacing it with “next: { Optional(foo($0)) }” gives the result I originally expected.

This actually would have made more sense to me if the signature was “func sequence<T>(first: T?, next: (T) -> T?) -> UnfoldFirstSequence<T>”, and “let a = nil" would have caused an empty sequence.  I understand that this is a situation that would change unexpectedly if this stdlib change occurred, but I sure think the changed sequence(first:next:) function makes for a more understandable result.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160816/4f2904b4/attachment.html>

More information about the swift-evolution mailing list