<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div><br></div><div>On Dec 14, 2016, at 09:54, Jordan Rose &lt;<a href="mailto:jordan_rose@apple.com">jordan_rose@apple.com</a>&gt; wrote:<br><br></div><blockquote type="cite"><meta http-equiv="Content-Type" content="text/html charset=utf-8"><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 13, 2016, at 20:43, David Sweeris via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><blockquote type="cite" class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;"><br class="Apple-interchange-newline">On Dec 13, 2016, at 9:51 AM, Chris Lattner &lt;<a href="mailto:clattner@apple.com" class="">clattner@apple.com</a>&gt; wrote:<br class=""><br class="">On Dec 12, 2016, at 6:58 PM, David Sweeris via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><blockquote type="cite" class=""><blockquote type="cite" class="">On Dec 12, 2016, at 16:15, John Holdsworth via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class="">I’d like to raise again the idea of optionality when referencing a key or<br class="">calling a function could be possible using a ? i.e instead of<br class=""><br class="">&nbsp;let a = key != nil ? dict[key] : nil<br class=""><br class="">you could just write:<br class=""><br class="">&nbsp;let a = dict[key?]<br class=""><br class="">or even&nbsp;<br class=""><br class="">&nbsp;let a = func( arg: argumentThatMayBeNull? ) // not called if argument is nil<br class=""></blockquote><br class="">The first part is pretty easy to add in an extension:<br class=""><br class="">extension Dictionary {<br class="">&nbsp; subscript(_ key:Key?) -&gt; Value? {<br class="">&nbsp; &nbsp; &nbsp; return key != nil ? self[key!] : nil<br class="">&nbsp; }<br class="">}<br class=""><br class="">At least I think that works... I'm on my phone so I can't test it.<br class=""></blockquote><br class="">You can do something like this, but I’d recommend labeling the subscript. &nbsp;The problem comes up when you have a dictionary that has an optional key: &nbsp; When you use “myDict[nil]”, you may get one or the other, but you probably mean one specifically. &nbsp;<br class=""></blockquote><div class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">I don’t think that’s an issue in the stdlib, because `Optional` doesn’t conform to `Hashable` and, AFAIK, no other stdlib types conform to `ExpressibleByNilLiteral`. Custom types could conform to both, though, and according to a playground, that does indeed lead to some confusing code:</div><blockquote class="" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div class=""><div class="" style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">struct</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space">&nbsp;</span>Foo :<span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures;">ExpressibleByNilLiteral</span><span class="" style="font-variant-ligatures: no-common-ligatures;">,<span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures;">Hashable</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space">&nbsp;</span>{</span>...}</div></div><div class=""><div class="" style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">extension</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures;">Dictionary</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space">&nbsp;</span>{</span><span class="" style="font-variant-ligatures: no-common-ligatures;">&nbsp;</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">subscript</span><span class="" style="font-variant-ligatures: no-common-ligatures;">(</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">_</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space">&nbsp;</span>key:</span><span class="" style="font-variant-ligatures: no-common-ligatures;">Key</span><span class="" style="font-variant-ligatures: no-common-ligatures;">?) -&gt;<span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures;">Value</span><span class="" style="font-variant-ligatures: no-common-ligatures;">? {&nbsp;</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">return</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space">&nbsp;</span>key !=<span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">nil</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space">&nbsp;</span>?<span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">self</span><span class="" style="font-variant-ligatures: no-common-ligatures;">[key!] :<span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">nil</span>&nbsp;} }</div></div><div class=""><div class="" style="margin: 0px; line-height: normal; font-family: Menlo;"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">var</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space">&nbsp;</span>bar = [</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">Foo</span><span class="" style="font-variant-ligatures: no-common-ligatures;">:</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">Int</span><span class="" style="font-variant-ligatures: no-common-ligatures;">]()</span></div></div><div class=""><div class="" style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">bar</span><span class="" style="font-variant-ligatures: no-common-ligatures;">[</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">nil</span><span class="" style="font-variant-ligatures: no-common-ligatures;">]<span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures;">//calls `Foo.init(nilLiteral:())`, and tries to look up the new `Foo` in `bar` using the stdlib's subscript</span></div></div><div class=""><div class="" style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">bar</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(49, 89, 93);">[</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">nil</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">as</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(79, 129, 135);">Foo</span><span class="" style="font-variant-ligatures: no-common-ligatures;">?</span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(49, 89, 93);">]</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space">&nbsp;</span></span><span class="" style="font-variant-ligatures: no-common-ligatures;">//passes `Optional&lt;Foo&gt;.none, which uses the extension's subscript</span></div></div></blockquote></div></blockquote><br class=""></div><div>I wouldn't be surprised if there's a proposal to make Optional conditionally conform to Hashable if/when we get conditional conformances.</div></blockquote><br><div>Probably, yeah... I'll be curious as to how they propose we prevent <span style="background-color: rgba(255, 255, 255, 0);">collisions&nbsp;between&nbsp;</span>Optional&lt;T&gt;.none.hashValue and any given &nbsp;T's hash value.</div><div><br></div><div>- Dave Sweeris&nbsp;</div></body></html>