<div dir="ltr">On Tue, Aug 16, 2016 at 7:34 PM, Braeden Profile via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Okay, I’m actually confused about the current state of things.<div><br></div><div>Earlier, this was introduced:</div><div><br></div><div><span class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(146,146,146)"><span>// This function is silly,</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(146,146,146)"><span>// but useful functions may take a similar form</span></div></span><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#008f01">func</span><span> foo(x: </span><span style="color:#5b2699">Int</span><span>?) -> </span><span style="color:#5b2699">Int</span><span>? {</span></div><span class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,143,1)"><span style="color:#000000"> </span><span>guard</span><span style="color:#000000"> </span><span>let</span><span style="color:#000000"> x = x </span><span>else</span><span style="color:#000000"> { </span><span>return</span><span style="color:#000000"> </span><span style="color:#da2000">0</span><span style="color:#000000"> }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span> </span><span style="color:#008f01">return</span><span> x > </span><span style="color:#da2000">12</span><span> ? </span><span style="color:#008f01">nil</span><span> : x + </span><span style="color:#da2000">1</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#008f01">let</span><span> a: </span><span style="color:#5b2699">Int</span><span>? = </span><span style="color:#008f01">nil</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:#008f01">for</span><span> i </span><span style="color:#008f01">in</span><span> </span><span style="color:rgb(49,89,93)">sequence</span><span>(first: a, next: { </span><span style="color:rgb(49,89,93)">foo</span><span>($0) })</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>{</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(146,146,146)"><span style="color:#000000"> </span><span>// This is a pretty useless thing to do,</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(146,146,146)"><span style="color:#000000"> </span><span>// but there are useful things that can be done</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(146,146,146)"><span style="color:#000000"> </span><span>// without checking whether `i == nil`</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span> </span><span style="color:#3d1d81">print</span><span>(i)</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>}</span></div></span></div><div><span><span style="font-family:Helvetica;font-size:12px"><br></span></span></div><div style="margin:0px;line-height:normal">…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?</div></div></blockquote><div><br></div><div>Yeah, I think that's a bug :) Which means it should be fixed whether we change the function signature or not.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div style="margin:0px;line-height:normal">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.</div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal">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.</div></div><br>______________________________<wbr>_________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br>
<br></blockquote></div><br></div></div>