<div dir="ltr">That's a good point in Jay's example and from what I can tell a good way to address it, Haravikk. <div><br></div><div>I've done some work in a language that only provides type narrowing for immutable types (that'd be Kotlin as I've mentioned before) and whenever I've used it it feels like the only thing it's really been missing is the "if let" construct allowing you to bind and unwrap mutable values, which leads me to think that synergistically, in Swift, this would be fantastic. The main benefit probably is that it would allow code to read much better.</div><div><br></div><div>I think the "type stack" phrasing in the proposal is throwing some people for a loop making them think it'll have way more mental overhead than it actually does in practice...really, in practice, I've used this either (a) for checking nullity or (b) for checking for a specific subclass. There's little to no mental overhead in either of those cases, as your "type stack" is simply 2-long (this was an Int? outside of this conditional block, and it's an Int inside. This was a UIViewController outside of this conditional block, and it's a UINavigationController inside.)</div><div><br></div><div>While it may not be a panacea that allows new applications that no one could have thought of, I have no doubt that it would greatly improve the experience of coding in the language by, as you said, allowing more flexibility in expressiveness. Regardless of one's opinion on the efficacy of this feature as a whole, there *are* frequent situations where this feature leads to substantially better-reading code. The "unwrap" keyword proposed in another thread, critically, solves only <b>half</b> of the problems that this proposal solves (as far as I could tell from reading a few emails in the chain, it left the subclass inference completely untouched). Ability to say "if (controller is SongSelectionViewController) { controller.search(for: "mozart") }" is mentally freeing and helps me stay in coder zen. </div><div><br></div><div>IMO, it would effectively be the cream cheese icing on the top of the carrot cake of Swift's unwrapping and type inference features. A good tasteful cream cheese icing always improves a carrot cake. </div><div><br></div><div>The question then becomes, is it really worth the implementation time? This is something that, presumably, someone from the Swift team would need to be involved in answering. </div></div><br><div class="gmail_quote"><div dir="ltr">On Fri, Nov 11, 2016 at 10:52 AM Haravikk via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br></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" class="gmail_msg"><br class="gmail_msg"><div class="gmail_msg"><blockquote type="cite" class="gmail_msg"><div class="gmail_msg">On 10 Nov 2016, at 21:42, Jay Abbott <<a href="mailto:jay@abbott.me.uk" class="gmail_msg" target="_blank">jay@abbott.me.uk</a>> wrote:</div><br class="m_-8833943004122056022Apple-interchange-newline gmail_msg"><div class="gmail_msg"><div dir="ltr" class="gmail_msg"><div class="m_-8833943004122056022markdown-here-wrapper gmail_msg"><p style="margin:0px 0px 1.2em!important" class="gmail_msg">Consider this code:</p>
<pre style="font-size:1em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px" class="gmail_msg"><code class="m_-8833943004122056022language-swift m_-8833943004122056022hljs gmail_msg" style="font-size:1em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline;white-space:pre-wrap;overflow:auto;border-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block!important;display:block;overflow-x:auto;padding:0.5em;color:rgb(51,51,51);background:rgb(248,248,248) none repeat scroll 0% 0%"><span class="m_-8833943004122056022hljs-class gmail_msg"><span class="m_-8833943004122056022hljs-keyword gmail_msg" style="color:rgb(51,51,51);font-weight:bold">struct</span> <span class="m_-8833943004122056022hljs-title gmail_msg" style="color:rgb(153,0,0);font-weight:bold;color:rgb(68,85,136);font-weight:bold">Pet</span> </span>{
<span class="m_-8833943004122056022hljs-keyword gmail_msg" style="color:rgb(51,51,51);font-weight:bold">let</span> name: <span class="m_-8833943004122056022hljs-type gmail_msg" style="color:rgb(68,85,136);font-weight:bold">String</span>
<span class="m_-8833943004122056022hljs-keyword gmail_msg" style="color:rgb(51,51,51);font-weight:bold">weak</span> <span class="m_-8833943004122056022hljs-keyword gmail_msg" style="color:rgb(51,51,51);font-weight:bold">var</span> owner: <span class="m_-8833943004122056022hljs-type gmail_msg" style="color:rgb(68,85,136);font-weight:bold">Person</span>?
<span class="m_-8833943004122056022hljs-keyword gmail_msg" style="color:rgb(51,51,51);font-weight:bold">init</span>(name: <span class="m_-8833943004122056022hljs-type gmail_msg" style="color:rgb(68,85,136);font-weight:bold">String</span>, owner: <span class="m_-8833943004122056022hljs-type gmail_msg" style="color:rgb(68,85,136);font-weight:bold">Person</span>?) {
<span class="m_-8833943004122056022hljs-keyword gmail_msg" style="color:rgb(51,51,51);font-weight:bold">self</span>.name = name
<span class="m_-8833943004122056022hljs-keyword gmail_msg" style="color:rgb(51,51,51);font-weight:bold">self</span>.owner = owner
owner?.pet = <span class="m_-8833943004122056022hljs-keyword gmail_msg" style="color:rgb(51,51,51);font-weight:bold">self</span>
}
<span class="m_-8833943004122056022hljs-keyword gmail_msg" style="color:rgb(51,51,51);font-weight:bold">mutating</span> <span class="m_-8833943004122056022hljs-func gmail_msg"><span class="m_-8833943004122056022hljs-keyword gmail_msg" style="color:rgb(51,51,51);font-weight:bold">func</span> <span class="m_-8833943004122056022hljs-title gmail_msg" style="color:rgb(153,0,0);font-weight:bold">transferOwnership</span><span class="m_-8833943004122056022hljs-params gmail_msg">(to newOwner: Person)</span> </span>{
<span class="m_-8833943004122056022hljs-keyword gmail_msg" style="color:rgb(51,51,51);font-weight:bold">let</span> previousOwner = owner
owner = newOwner
newOwner.pet = <span class="m_-8833943004122056022hljs-keyword gmail_msg" style="color:rgb(51,51,51);font-weight:bold">self</span>
<span class="m_-8833943004122056022hljs-keyword gmail_msg" style="color:rgb(51,51,51);font-weight:bold">if</span>(previousOwner != <span class="m_-8833943004122056022hljs-built_in gmail_msg" style="color:rgb(0,134,179)">nil</span>) {
previousOwner!.pet = <span class="m_-8833943004122056022hljs-built_in gmail_msg" style="color:rgb(0,134,179)">nil</span>
}
}
<span class="m_-8833943004122056022hljs-func gmail_msg"><span class="m_-8833943004122056022hljs-keyword gmail_msg" style="color:rgb(51,51,51);font-weight:bold">func</span> <span class="m_-8833943004122056022hljs-title gmail_msg" style="color:rgb(153,0,0);font-weight:bold">feed</span><span class="m_-8833943004122056022hljs-params gmail_msg">()</span> </span>{
}
}
<span class="m_-8833943004122056022hljs-class gmail_msg"><span class="m_-8833943004122056022hljs-keyword gmail_msg" style="color:rgb(51,51,51);font-weight:bold">class</span> <span class="m_-8833943004122056022hljs-title gmail_msg" style="color:rgb(153,0,0);font-weight:bold;color:rgb(68,85,136);font-weight:bold">Person</span> </span>{
<span class="m_-8833943004122056022hljs-keyword gmail_msg" style="color:rgb(51,51,51);font-weight:bold">let</span> name: <span class="m_-8833943004122056022hljs-type gmail_msg" style="color:rgb(68,85,136);font-weight:bold">String</span>
<span class="m_-8833943004122056022hljs-keyword gmail_msg" style="color:rgb(51,51,51);font-weight:bold">var</span> pet: <span class="m_-8833943004122056022hljs-type gmail_msg" style="color:rgb(68,85,136);font-weight:bold">Pet</span>?
<span class="m_-8833943004122056022hljs-keyword gmail_msg" style="color:rgb(51,51,51);font-weight:bold">init</span>(name: <span class="m_-8833943004122056022hljs-type gmail_msg" style="color:rgb(68,85,136);font-weight:bold">String</span>) {
<span class="m_-8833943004122056022hljs-keyword gmail_msg" style="color:rgb(51,51,51);font-weight:bold">self</span>.name = name
}
<span class="m_-8833943004122056022hljs-func gmail_msg"><span class="m_-8833943004122056022hljs-keyword gmail_msg" style="color:rgb(51,51,51);font-weight:bold">func</span> <span class="m_-8833943004122056022hljs-title gmail_msg" style="color:rgb(153,0,0);font-weight:bold">givePetAway</span><span class="m_-8833943004122056022hljs-params gmail_msg">(to someone: Person)</span> </span>{
<span class="m_-8833943004122056022hljs-keyword gmail_msg" style="color:rgb(51,51,51);font-weight:bold">if</span> pet != <span class="m_-8833943004122056022hljs-built_in gmail_msg" style="color:rgb(0,134,179)">nil</span> {
pet!.transferOwnership(to: someone)
<span class="m_-8833943004122056022hljs-comment gmail_msg" style="color:rgb(153,153,136);font-style:italic">//pet!.feed()</span>
}
}
}
<span class="m_-8833943004122056022hljs-keyword gmail_msg" style="color:rgb(51,51,51);font-weight:bold">let</span> bert = <span class="m_-8833943004122056022hljs-type gmail_msg" style="color:rgb(68,85,136);font-weight:bold">Person</span>(name: <span class="m_-8833943004122056022hljs-string gmail_msg" style="color:rgb(221,17,68)">"Bert"</span>)
<span class="m_-8833943004122056022hljs-keyword gmail_msg" style="color:rgb(51,51,51);font-weight:bold">let</span> ernie = <span class="m_-8833943004122056022hljs-type gmail_msg" style="color:rgb(68,85,136);font-weight:bold">Person</span>(name: <span class="m_-8833943004122056022hljs-string gmail_msg" style="color:rgb(221,17,68)">"Ernie"</span>)
<span class="m_-8833943004122056022hljs-keyword gmail_msg" style="color:rgb(51,51,51);font-weight:bold">var</span> elmo = <span class="m_-8833943004122056022hljs-type gmail_msg" style="color:rgb(68,85,136);font-weight:bold">Pet</span>(name: <span class="m_-8833943004122056022hljs-string gmail_msg" style="color:rgb(221,17,68)">"Elmo"</span>, owner: <span class="m_-8833943004122056022hljs-built_in gmail_msg" style="color:rgb(0,134,179)">nil</span>)
elmo.transferOwnership(to: bert)
<span class="m_-8833943004122056022hljs-built_in gmail_msg" style="color:rgb(0,134,179)">print</span>(<span class="m_-8833943004122056022hljs-string gmail_msg" style="color:rgb(221,17,68)">"Bert's pet is <span class="m_-8833943004122056022hljs-subst gmail_msg" style="color:rgb(51,51,51);font-weight:bold;font-weight:normal">\(bert.pet)</span> - Ernie's pet is <span class="m_-8833943004122056022hljs-subst gmail_msg" style="color:rgb(51,51,51);font-weight:bold;font-weight:normal">\(ernie.pet)</span>"</span>)
bert.givePetAway(to: ernie)
<span class="m_-8833943004122056022hljs-built_in gmail_msg" style="color:rgb(0,134,179)">print</span>(<span class="m_-8833943004122056022hljs-string gmail_msg" style="color:rgb(221,17,68)">"Bert's pet is <span class="m_-8833943004122056022hljs-subst gmail_msg" style="color:rgb(51,51,51);font-weight:bold;font-weight:normal">\(bert.pet)</span> - Ernie's pet is <span class="m_-8833943004122056022hljs-subst gmail_msg" style="color:rgb(51,51,51);font-weight:bold;font-weight:normal">\(ernie.pet)</span>"</span>)
</code></pre><p style="margin:0px 0px 1.2em!important" class="gmail_msg">This works as expected, but if you uncomment <code style="font-size:1em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline" class="gmail_msg">pet!.feed()</code> in <code style="font-size:1em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline" class="gmail_msg">givePetAway(to:)</code> it will crash, because the mutating function modifies the two-way relationship between <code style="font-size:1em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline" class="gmail_msg">pet</code> and <code style="font-size:1em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline" class="gmail_msg">owner</code>.</p><p style="margin:0px 0px 1.2em!important" class="gmail_msg">In the code I use <code style="font-size:1em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline" class="gmail_msg">if pet != nil</code> to demonstrate, in your proposal for <code style="font-size:1em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline" class="gmail_msg">unwrap</code>, if I used it to <code style="font-size:1em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline" class="gmail_msg">unwrap pet</code> (a value-type, but accessed through <code style="font-size:1em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline" class="gmail_msg">self</code> so it can be modified after unwrapping because it’s not nil at the moment) the compiler would assume I could use it and <code style="font-size:1em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline" class="gmail_msg">pet.feed()</code> would crash, just as <code style="font-size:1em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline" class="gmail_msg">pet!.feed()</code> does now. In your proposal for type narrowing, it would be the same problem. This is like your <code style="font-size:1em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline" class="gmail_msg">foo.value</code> example from the proposal.</p><p style="margin:0px 0px 1.2em!important" class="gmail_msg">I don’t think you can get around the fact that the compiler can’t guarantee a type-narrowed or even unwrapped mutable value, which is why <code style="font-size:1em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline" class="gmail_msg">if let</code> works as it does with an immutable snapshot.</p><p style="margin:0px 0px 1.2em!important" class="gmail_msg">However, type narrowing for immutable values would still be good.</p></div></div></div></blockquote></div></div><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg">This isn't a problem of mutability, it's a problem due to the use of a reference type (Person); this is what the classes and concurrency section is supposed to be describing, but perhaps I haven't made it clear enough. So in the example you've given self.pet can't be unwrapped because self is a reference type, thus you need to either use force unwrapping either with the unwrap! keyword or direct force unwrapping like you've used (since behind the scenes it's the same thing).</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">You are right though that the resulting error isn't necessarily a concurrency issue, so I'll make a note of this for the proposed new error, and also clarify that the reference type restriction doesn't just apply to the property itself, but also to whatever it belongs to (and so-on up the chain, so if any part of foo.bar.a.b.c is a reference type it can't be soft unwrapped).</div></div>_______________________________________________<br class="gmail_msg">
swift-evolution mailing list<br class="gmail_msg">
<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="gmail_msg">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="gmail_msg">
</blockquote></div>