<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="">I remember discussing something like this in swift-evolution but can’t find it. I was wondering if it was worth a proposal or not.<div class=""><br class=""></div><div class="">When constructing objects from dictionary of values, I often write the parsing of optional values as such:</div><div class=""><br class=""></div><div class=""><font face="Menlo" style="font-size: 12px;" class="">age&nbsp;=&nbsp;try&nbsp;dictionary["age"].flatMap&nbsp;{<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>if&nbsp;let&nbsp;age = $0&nbsp;as?&nbsp;Int&nbsp;{<br class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>return&nbsp;age<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>}&nbsp;else&nbsp;{<br class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>throw&nbsp;</font><span style="font-family: Menlo; font-size: 12px;" class="">Error</span><span style="font-family: Menlo; font-size: 12px;" class="">.</span><span style="font-family: Menlo; font-size: 12px;" class="">InvalidFormat</span><font face="Menlo" style="font-size: 12px;" class=""><br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>}<br class="">}</font></div><div class=""><font face="Menlo" style="font-size: 12px;" class=""><br class=""></font></div><div class="">Basically, I don’t want to throw if the dictionary does not contain an “age” key, but if it does, I want to throw if it is not an Int. I’m writing this way, but I’d like to write is as such:</div><div class=""><br class=""></div><div class=""><span style="font-family: Menlo; font-size: 12px;" class="">age</span><span style="font-family: Menlo; font-size: 12px;" class="">&nbsp;=&nbsp;</span><span style="font-family: Menlo; font-size: 12px;" class="">try</span><span style="font-family: Menlo; font-size: 12px;" class="">&nbsp;dictionary[</span><span style="font-family: Menlo; font-size: 12px;" class="">"age"</span><span style="font-family: Menlo; font-size: 12px;" class="">].</span><span style="font-family: Menlo; font-size: 12px;" class="">flatMap</span><span style="font-family: Menlo; font-size: 12px;" class="">&nbsp;{ $0 as? Int ??</span>&nbsp;<span style="font-family: Menlo; font-size: 12px;" class="">throw</span><span style="font-family: Menlo; font-size: 12px;" class="">&nbsp;</span><span style="font-family: Menlo; font-size: 12px;" class="">Error</span><span style="font-family: Menlo; font-size: 12px;" class="">.</span><span style="font-family: Menlo; font-size: 12px;" class="">InvalidFormat }</span><br class=""></div><div class=""><span style="font-family: Menlo; font-size: 12px;" class=""><br class=""></span></div><div class="">But this causes a compilation error. Don’t you think the ternary operator should allow an expression that throws on the right hand side?</div><div class=""><br class=""></div><div class="">David.</div></body></html>