<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">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><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><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>_______________________________________________<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>