<html><head><style>body{font-family:Helvetica,Arial;font-size:13px}</style></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div><blockquote type="cite" class="clean_bq" style="font-family: Helvetica, Arial; font-size: 13px; 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;"><span><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><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></div></span></blockquote></div><p>For one, ?? is not a ternary operator, ?: is. But I guess it’s just a misspelling. </p><p>Anyway, there’re the throwing versions of the ?? operator:</p><p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Monaco;"><span style="font-variant-ligatures: no-common-ligatures;">public</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures;">func</span><span style="font-variant-ligatures: no-common-ligatures;"> ??<T>(optional: T?, </span><span style="font-variant-ligatures: no-common-ligatures;">@autoclosure</span><span style="font-variant-ligatures: no-common-ligatures;"> defaultValue: () </span><span style="font-variant-ligatures: no-common-ligatures;">throws</span><span style="font-variant-ligatures: no-common-ligatures;"> -> T) </span><span style="font-variant-ligatures: no-common-ligatures;">rethrows</span><span style="font-variant-ligatures: no-common-ligatures;"> -> </span><span style="font-variant-ligatures: no-common-ligatures;">T</span></p><p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Monaco; min-height: 19px;"><span style="font-variant-ligatures: no-common-ligatures;">public</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures;">func</span><span style="font-variant-ligatures: no-common-ligatures;"> ??<T>(optional: T?, </span><span style="font-variant-ligatures: no-common-ligatures;">@autoclosure</span><span style="font-variant-ligatures: no-common-ligatures;"> defaultValue: () </span><span style="font-variant-ligatures: no-common-ligatures;">throws</span><span style="font-variant-ligatures: no-common-ligatures;"> -> T?) </span><span style="font-variant-ligatures: no-common-ligatures;">rethrows</span><span style="font-variant-ligatures: no-common-ligatures;"> -> </span><span style="font-variant-ligatures: no-common-ligatures;">T</span><span style="font-variant-ligatures: no-common-ligatures;">?</span><span style="font-variant-ligatures: no-common-ligatures"></span></p><p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Monaco; color: rgb(164, 176, 177);"><span style="font-variant-ligatures: no-common-ligatures"><span style="color: rgb(0, 0, 0); font-family: Helvetica, Arial; font-size: 13px;"><br></span></span></p><p style="margin: 0px; line-height: normal;">They’re taking the throwing autoclosure as second parameter. You cannot pass `throw Error.InvalidFormat` directly, because it’s type is not resolved to `() -> throws Int` by compiler. Nos sure if it’s a bug or a feature. In either case, you can work around it by wrapping throw in closure:</p><p style="margin: 0px; line-height: normal;"><br></p><p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Monaco;"><span style="font-variant-ligatures: no-common-ligatures;">let</span><span style="font-variant-ligatures: no-common-ligatures;"> age = </span><span style="font-variant-ligatures: no-common-ligatures;">try</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures;">dictionary</span><span style="font-variant-ligatures: no-common-ligatures;">[</span><span style="font-variant-ligatures: no-common-ligatures;">"age"</span><span style="font-variant-ligatures: no-common-ligatures;">].</span><span style="font-variant-ligatures: no-common-ligatures;">flatMap</span><span style="font-variant-ligatures: no-common-ligatures;"> { elem </span><span style="font-variant-ligatures: no-common-ligatures;">in</span></p><p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Monaco;"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures;">try</span><span style="font-variant-ligatures: no-common-ligatures"> elem </span><span style="font-variant-ligatures: no-common-ligatures;">as</span><span style="font-variant-ligatures: no-common-ligatures">? </span><span style="font-variant-ligatures: no-common-ligatures;">Int</span><span style="font-variant-ligatures: no-common-ligatures"> ?? { </span><span style="font-variant-ligatures: no-common-ligatures;">throw</span><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures;">Error</span><span style="font-variant-ligatures: no-common-ligatures">() }()</span></p><p style="margin: 0px; line-height: normal;">
</p><p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Monaco;"><span style="font-variant-ligatures: no-common-ligatures;">}</span></p><p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Monaco;"><span style="font-variant-ligatures: no-common-ligatures;"><span style="font-family: Helvetica, Arial; font-size: 13px;"><br></span></span></p><p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Monaco;"><span style="font-variant-ligatures: no-common-ligatures;"><span style="font-family: Helvetica, Arial; font-size: 13px;">You can also simplify it using a throwing function, possibly from the Error enum:</span></span></p><p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Monaco;"><span style="font-variant-ligatures: no-common-ligatures;"><span style="font-family: Helvetica, Arial; font-size: 13px;"><br></span></span></p><p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Monaco;"><span style="font-variant-ligatures: no-common-ligatures;">enum</span><span style="font-variant-ligatures: no-common-ligatures;"> Error : </span><span style="font-variant-ligatures: no-common-ligatures;">ErrorType</span><span style="font-variant-ligatures: no-common-ligatures;"> {</span></p><p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Monaco;"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures;">case</span><span style="font-variant-ligatures: no-common-ligatures"> invalidFormat</span></p><p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Monaco;"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures;">static</span><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures;">func</span><span style="font-variant-ligatures: no-common-ligatures"> throwInvalidFormat<T>() </span><span style="font-variant-ligatures: no-common-ligatures;">throws</span><span style="font-variant-ligatures: no-common-ligatures"> -> </span><span style="font-variant-ligatures: no-common-ligatures;">T</span><span style="font-variant-ligatures: no-common-ligatures"> {</span></p><p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Monaco;"><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures;">throw</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures;">Error</span><span style="font-variant-ligatures: no-common-ligatures;">.</span><span style="font-variant-ligatures: no-common-ligatures">invalidFormat</span></p><p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Monaco;"><span style="font-variant-ligatures: no-common-ligatures"> }</span></p><p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Monaco;"><span style="font-variant-ligatures: no-common-ligatures;">
</span></p><p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Monaco;"><span style="font-variant-ligatures: no-common-ligatures;">}</span></p><p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Monaco;"><span style="font-variant-ligatures: no-common-ligatures;"><br></span></p><p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Monaco;"><span style="font-variant-ligatures: no-common-ligatures;">let</span><span style="font-variant-ligatures: no-common-ligatures;"> age = </span><span style="font-variant-ligatures: no-common-ligatures;">try</span><span style="font-variant-ligatures: no-common-ligatures;"> </span><span style="font-variant-ligatures: no-common-ligatures;">dictionary</span><span style="font-variant-ligatures: no-common-ligatures;">[</span><span style="font-variant-ligatures: no-common-ligatures;">"age"</span><span style="font-variant-ligatures: no-common-ligatures;">].</span><span style="font-variant-ligatures: no-common-ligatures;">flatMap</span><span style="font-variant-ligatures: no-common-ligatures;"> { elem </span><span style="font-variant-ligatures: no-common-ligatures;">in</span></p><p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Monaco;"><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="font-variant-ligatures: no-common-ligatures;">try</span><span style="font-variant-ligatures: no-common-ligatures"> elem </span><span style="font-variant-ligatures: no-common-ligatures;">as</span><span style="font-variant-ligatures: no-common-ligatures">? </span><span style="font-variant-ligatures: no-common-ligatures;">Int</span><span style="font-variant-ligatures: no-common-ligatures"> ?? </span><span style="font-variant-ligatures: no-common-ligatures;">Error</span><span style="font-variant-ligatures: no-common-ligatures">.</span><span style="font-variant-ligatures: no-common-ligatures;">throwInvalidFormat</span><span style="font-variant-ligatures: no-common-ligatures">()</span></p><p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Monaco;"><span style="font-variant-ligatures: no-common-ligatures;">
</span></p><p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Monaco;"><span style="font-variant-ligatures: no-common-ligatures;">}</span></p><p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Monaco;"><span style="font-variant-ligatures: no-common-ligatures;"><span style="font-family: Helvetica, Arial; font-size: 13px;"><br></span></span></p><p style="margin: 0px; line-height: normal;">Cheers,</p><p style="margin: 0px; line-height: normal;">Krzysztof</p><p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Monaco;"><br></p><p style="margin: 0px; font-size: 14px; line-height: normal; font-family: Monaco; color: rgb(164, 176, 177);"><br></p><div><blockquote type="cite" class="clean_bq" style="font-family: Helvetica, Arial; font-size: 13px; 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;"><span><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div><div class=""><br class="Apple-interchange-newline"><br class=""></div><div class="">David.</div>_______________________________________________<br>swift-evolution mailing list<br>swift-evolution@swift.org<br>https://lists.swift.org/mailman/listinfo/swift-evolution<br></div></div></span></blockquote></div></body></html>