<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><blockquote type="cite" class="">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="">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 style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">struct</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> Foo : </span><span style="font-variant-ligatures: no-common-ligatures" class="">ExpressibleByNilLiteral</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">, </span><span style="font-variant-ligatures: no-common-ligatures" class="">Hashable</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span>...}</div></div><div class=""><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">Dictionary</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> {</span><span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">subscript</span><span style="font-variant-ligatures: no-common-ligatures;" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">_</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> key:</span><span style="font-variant-ligatures: no-common-ligatures;" class="">Key</span><span style="font-variant-ligatures: no-common-ligatures;" class="">?) -&gt; </span><span style="font-variant-ligatures: no-common-ligatures;" class="">Value</span><span style="font-variant-ligatures: no-common-ligatures;" class="">? {&nbsp;</span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">return</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> key != </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">nil</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> ? </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">self</span><span style="font-variant-ligatures: no-common-ligatures;" class="">[key!] : </span><span style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">nil</span>&nbsp;} }</div></div><div class=""><div style="margin: 0px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">var</span><span style="font-variant-ligatures: no-common-ligatures" class=""> bar = [</span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Foo</span><span style="font-variant-ligatures: no-common-ligatures" class="">:</span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures" class="">]()</span></div></div><div class=""><div style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">bar</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">[</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">] </span><span style="font-variant-ligatures: no-common-ligatures" class="">//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 style="margin: 0px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">bar</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">[</span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">nil</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #bb2ca2" class="">as</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">Foo</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class="">?</span><span style="font-variant-ligatures: no-common-ligatures; color: #31595d" class="">]</span><span style="font-variant-ligatures: no-common-ligatures; color: #000000" class=""> </span><span style="font-variant-ligatures: no-common-ligatures" class="">//passes `Optional&lt;Foo&gt;.none, which uses the extension's subscript</span></div></div></blockquote><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><br class=""></div><blockquote type="cite" class="">Using a label on the subscript solves this, and makes the code more explicit that you’re not just getting the normal subscript that everyone would expect.</blockquote><div class="">Yeah, that would certainly solve it. Kind of a shame, though, since it’d be one less function to think about, and 99.998% of the time it’d give the right answer. Too bad we can’t extend stuff where some condition <i class="">isn’t</i> met, like "<span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">extension</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);" class="">Dictionary</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class=""> </span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);" class="">where</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;" class="">&nbsp;!(Key: ExpressibleByNilLiteral) {…}</span><span style="font-variant-ligatures: no-common-ligatures;" class="">” or something.</span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">- Dave Sweeris</span></div></body></html>