<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Dec 13, 2016, at 20:43, David Sweeris via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> 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 <<a href="mailto:clattner@apple.com" class="">clattner@apple.com</a>> wrote:<br class=""><br class="">On Dec 12, 2016, at 6:58 PM, David Sweeris via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""><blockquote type="cite" class=""><blockquote type="cite" class="">On Dec 12, 2016, at 16:15, John Holdsworth via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> 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=""> let a = key != nil ? dict[key] : nil<br class=""><br class="">you could just write:<br class=""><br class=""> let a = dict[key?]<br class=""><br class="">or even <br class=""><br class=""> 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=""> subscript(_ key:Key?) -> Value? {<br class=""> return key != nil ? self[key!] : nil<br class=""> }<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. The problem comes up when you have a dictionary that has an optional key: When you use “myDict[nil]”, you may get one or the other, but you probably mean one specifically. <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"> </span>Foo :<span class="Apple-converted-space"> </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"> </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"> </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"> </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"> </span>{</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);">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"> </span>key:</span><span class="" style="font-variant-ligatures: no-common-ligatures;">Key</span><span class="" style="font-variant-ligatures: no-common-ligatures;">?) -><span class="Apple-converted-space"> </span></span><span class="" style="font-variant-ligatures: no-common-ligatures;">Value</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);">return</span><span class="" style="font-variant-ligatures: no-common-ligatures;"><span class="Apple-converted-space"> </span>key !=<span class="Apple-converted-space"> </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"> </span>?<span class="Apple-converted-space"> </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"> </span></span><span class="" style="font-variant-ligatures: no-common-ligatures; color: rgb(187, 44, 162);">nil</span> } }</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"> </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"> </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"> </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"> </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"> </span></span><span class="" style="font-variant-ligatures: no-common-ligatures;">//passes `Optional<Foo>.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><div><br class=""></div><div>Jordan</div><br class=""></body></html>