<div dir="ltr">On Tue, Aug 16, 2016 at 7:44 PM, Xiaodi Wu <span dir="ltr">&lt;<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.com</a>&gt;</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 dir="ltr"><span class="">On Tue, Aug 16, 2016 at 7:34 PM, Braeden Profile via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br></span><div class="gmail_extra"><div class="gmail_quote"><span class=""><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><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>?) -&gt; </span><span style="color:#5b2699">Int</span><span>? {</span></div><span><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 &gt; </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?) -&gt; Int??) to raise (foo($0)) to (Optional(foo($0))) to avoid having the type signature end up being (Int?) -&gt; 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></span><div>Yeah, I think that&#39;s a bug :) Which means it should be fixed whether we change the function signature or not.</div></div></div></div></blockquote><div><br></div><div>I take that back. Not a bug. `next` would have type `@escaping (Int?) -&gt; Int??`.</div><div><br></div><div>When the result of `foo($0)` is `nil` (i.e. `Optional&lt;Int&gt;.none`) and that&#39;s used as the return value for `next`, it gets promoted to `Optional&lt;Optional&lt;Int&gt;&gt;.none`, not to `Optional&lt;Optional&lt;Int&gt;&gt;.some(nil)`. It makes sense, despite looking like gibberish.</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><div style="word-wrap:break-word"><div style="margin:0px;line-height:normal">And if you replace &quot;next: { foo($0) }&quot; with &quot;next: foo&quot;, 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&lt;T&gt;(first: T?, next: (T) -&gt; T?) -&gt; UnfoldFirstSequence&lt;T&gt;”, and “let a = nil&quot; 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></span><span class="">______________________________<wbr>_________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailma<wbr>n/listinfo/swift-evolution</a><br>
<br></span></blockquote></div><br></div></div>
</blockquote></div><br></div></div>