<div dir="ltr"><div class="gmail_default" style="font-family:georgia,serif">​I hope this will show something clearer to you. As you can see, y is defined as OptionalInt?, which is Int??.</div><div class="gmail_default" style="font-family:georgia,serif"><br></div><div class="gmail_default" style="font-family:georgia,serif">​<span style="font-family:Menlo;font-size:11px;color:rgb(187,44,162)">typealias</span><span style="font-family:Menlo;font-size:11px"> OptionInt = </span><span style="font-family:Menlo;font-size:11px;color:rgb(112,61,170)">Int</span><span style="font-family:Menlo;font-size:11px">?</span></div>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">func</span> someThrowingFunction() <span style="color:rgb(187,44,162)">throws</span> -&gt; <span style="color:rgb(79,129,135)">OptionInt</span> {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(0,0,0)">    </span><span style="color:rgb(187,44,162)">return</span><span style="color:rgb(0,0,0)"> </span>OptionInt<span style="color:rgb(0,0,0)">()</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(79,129,135)"><span style="color:rgb(187,44,162)">let</span><span style="color:rgb(0,0,0)"> y: </span>OptionInt?</p><div class="gmail_default" style="font-family:georgia,serif;display:inline">​​</div><p></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="color:rgb(187,44,162)">do</span> {</p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(49,89,93)"><span style="color:rgb(0,0,0)">    </span><span style="color:rgb(79,129,135)">y</span><span style="color:rgb(0,0,0)"> = </span><span style="color:rgb(187,44,162)">try</span><span style="color:rgb(0,0,0)"> </span>someThrowingFunction<span style="color:rgb(0,0,0)">()</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(187,44,162)"><span style="color:rgb(0,0,0)">} </span>catch<span style="color:rgb(0,0,0)"> {</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">    <span style="color:rgb(79,129,135)">y</span> = <span style="color:rgb(187,44,162)">nil</span></p>
<p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo">}</p><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><br></p><p style="margin:0px;line-height:normal"><font face="georgia, serif">z</font></p><div class="gmail_default" style="display:inline"><font face="georgia, serif">​haoxin​</font></div><p></p></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 25, 2016 at 11:54 PM, Jonathan Bailey via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div>So it would be legal to change the type of y in the second example to `Int??`<br><br>Does that mean when assigning optionals, it will unwrap, check if it&#39;s nill and assign nil, else assign the original value? This seems kind of pointless to just assigning the complete optional <br><br></div><div><br>On 25 Jan 2016, at 15:49, Svein Halvor Halvorsen &lt;<a href="mailto:svein.h@lvor.halvorsen.cc" target="_blank">svein.h@lvor.halvorsen.cc</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr">This is exactly according to the documentation.<div>In your first example `someThrowingFunction` returns an `Int`, so `y` is defined as an `Int?`.</div><div>In the second example `someThrowingFunction` returns an `Int?`, so `y` should be an `Int??`</div><div><br></div><div>However, since you didn&#39;t update the definition of `y` in your second example, the if branch either assigns an `Int?` to an `Int?`, which is legal, and may be nil, or it explicitly sets it to nil, which is also legal. Thus, effectively unwrapping the nested optionals.</div><div><br></div><div>Yu could also apply a `flatMap` to the nested optional, like so:</div><div><br></div><div>let x = (try? someThrowingFunction())?.flatMap({$0})</div><div><br></div><div>I&#39;m not sure if it&#39;s more readable, though.</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">2016-01-25 14:01 GMT+01:00 Jonathan Bailey via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</span>:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><div style="word-wrap:break-word">In the language guide on the apple website, <a href="https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html#//apple_ref/doc/uid/TP40014097-CH42-ID542" target="_blank">https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html#//apple_ref/doc/uid/TP40014097-CH42-ID542</a><div><br></div><div>It says the following assignments to x and y are equivalent:</div><div><ol style="border:0px;font-size:14px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;line-height:1.6em;list-style:none;font-family:Helvetica,Arial,sans-serif"><li style="border-width:0px 0px 0px 18px;border-left-style:solid;border-left-color:transparent;margin:0px;outline:0px;padding:0px 10px 0px 0px;vertical-align:baseline;white-space:pre-wrap;list-style-type:none"><code style="border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;font-family:Menlo,monospace;word-wrap:break-word"><span style="border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(170,51,145)">func</span> <span style="border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116)">someThrowingFunction</span>() <span style="border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(170,51,145)">throws</span> -&gt; <span style="border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(92,38,153)">Int</span> { ... }</code></li></ol></div><ol style="border:0px;font-size:14px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;line-height:1.6em;list-style:none;font-family:Helvetica,Arial,sans-serif"><li style="border-width:0px 0px 0px 18px;border-left-style:solid;border-left-color:transparent;margin:0px;outline:0px;padding:0px 10px 0px 0px;vertical-align:baseline;white-space:pre-wrap;list-style-type:none"><code style="border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;font-family:Menlo,monospace;word-wrap:break-word"><span style="border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(170,51,145)">let</span> <span style="border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116)">x</span> = <span style="border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(170,51,145)">try</span>? <span style="border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116)">someThrowingFunction</span>()</code></li><li style="border-width:0px 0px 0px 18px;border-left-style:solid;border-left-color:transparent;margin:0px;outline:0px;padding:0px 10px 0px 0px;vertical-align:baseline;white-space:pre-wrap;list-style-type:none"><code style="border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;font-family:Menlo,monospace;word-wrap:break-word"><span style="border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(0,131,18)">// x has type `Int?`</span></code></li><li style="border-width:0px 0px 0px 18px;border-left-style:solid;border-left-color:transparent;margin:0px;outline:0px;padding:0px 10px 0px 0px;vertical-align:baseline;white-space:pre-wrap;list-style-type:none"><span style="font-family:Menlo,monospace;font-size:0.85em"> </span></li><li style="border-width:0px 0px 0px 18px;border-left-style:solid;border-left-color:transparent;margin:0px;outline:0px;padding:0px 10px 0px 0px;vertical-align:baseline;white-space:pre-wrap;list-style-type:none"><code style="border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;font-family:Menlo,monospace;word-wrap:break-word"><span style="border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(170,51,145)">let</span> <span style="border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116)">y</span>: <span style="border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(92,38,153)">Int</span>?</code></li><li style="border-width:0px 0px 0px 18px;border-left-style:solid;border-left-color:transparent;margin:0px;outline:0px;padding:0px 10px 0px 0px;vertical-align:baseline;white-space:pre-wrap;list-style-type:none"><code style="border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;font-family:Menlo,monospace;word-wrap:break-word"><span style="border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(170,51,145)">do</span> {</code></li><li style="border-width:0px 0px 0px 18px;border-left-style:solid;border-left-color:transparent;margin:0px;outline:0px;padding:0px 10px 0px 0px;vertical-align:baseline;white-space:pre-wrap;list-style-type:none"><code style="border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;font-family:Menlo,monospace;word-wrap:break-word">    <span style="border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116)">y</span> = <span style="border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(170,51,145)">try</span> <span style="border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116)">someThrowingFunction</span>()</code></li><li style="border-width:0px 0px 0px 18px;border-left-style:solid;border-left-color:transparent;margin:0px;outline:0px;padding:0px 10px 0px 0px;vertical-align:baseline;white-space:pre-wrap;list-style-type:none"><code style="border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;font-family:Menlo,monospace;word-wrap:break-word">} <span style="border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(170,51,145)">catch</span> {</code></li><li style="border-width:0px 0px 0px 18px;border-left-style:solid;border-left-color:transparent;margin:0px;outline:0px;padding:0px 10px 0px 0px;vertical-align:baseline;white-space:pre-wrap;list-style-type:none"><code style="border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;font-family:Menlo,monospace;word-wrap:break-word">    <span style="border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116)">y</span> = <span style="border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(170,51,145)">nil</span></code></li><li style="border-width:0px 0px 0px 18px;border-left-style:solid;border-left-color:transparent;margin:0px;outline:0px;padding:0px 10px 0px 0px;vertical-align:baseline;white-space:pre-wrap;list-style-type:none"><code style="border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;font-family:Menlo,monospace;word-wrap:break-word">}</code></li></ol><div><span style="text-indent:0px"><br></span></div><div><span style="text-indent:0px">However this isn’t the case if someThrowingFunction also returns an optional, say:</span></div><div><span style="text-indent:0px"><br></span></div><div><ol style="border:0px;font-size:14px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;line-height:1.6em;list-style:none;font-family:Helvetica,Arial,sans-serif"><li style="border-width:0px 0px 0px 18px;border-left-style:solid;border-left-color:transparent;margin:0px;outline:0px;padding:0px 10px 0px 0px;vertical-align:baseline;white-space:pre-wrap;list-style-type:none"><code style="border:0px;font-size:0.85em;margin:0px;outline:0px;padding:0px;vertical-align:baseline;font-family:Menlo,monospace;word-wrap:break-word"><span style="border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(170,51,145)">func</span> <span style="border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(63,110,116)">someThrowingFunction</span>() <span style="border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(170,51,145)">throws</span> -&gt; <span style="border:0px;margin:0px;outline:0px;padding:0px;vertical-align:baseline;color:rgb(92,38,153)">Int?</span> { ... }</code></li></ol><div><font face="Menlo, monospace"><span style="white-space:pre-wrap"><br></span></font></div></div><div><span style="text-indent:0px">The type of x would be `Int??`, but the type of y is still `Int?`, is there some way to make the `try?` return an `Int?` instead of a double optional, which is not very helpful.</span></div><div><span style="text-indent:0px"><br></span></div><div><span style="text-indent:0px">Thanks,</span></div><div><span style="text-indent:0px">Jonathan</span></div><div><br></div></div><br></div></div>_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>
</div></blockquote></div><br>_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div>