<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 = try dictionary["age"].flatMap {<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>if let age = $0 as? Int {<br class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>return age<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>} else {<br class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>throw </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=""> = </span><span style="font-family: Menlo; font-size: 12px;" class="">try</span><span style="font-family: Menlo; font-size: 12px;" class=""> 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=""> { $0 as? Int ??</span> <span style="font-family: Menlo; font-size: 12px;" class="">throw</span><span style="font-family: Menlo; font-size: 12px;" class=""> </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>