<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="">Two more tiny overloads is all takes to fix it:<div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> ??&lt;T: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Error</span><span style="font-variant-ligatures: no-common-ligatures" class="">&gt;(lhs: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">?, rhs: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">) -&gt; </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">if</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> lhs = lhs { </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> lhs } </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">else</span><span style="font-variant-ligatures: no-common-ligatures" class=""> { </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> rhs }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class=""></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> ??&lt;T: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Error</span><span style="font-variant-ligatures: no-common-ligatures" class="">, U: </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Error</span><span style="font-variant-ligatures: no-common-ligatures" class="">&gt;(lhs: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">T</span><span style="font-variant-ligatures: no-common-ligatures" class="">?, rhs: </span><span style="font-variant-ligatures: no-common-ligatures; color: #4f8187" class="">U</span><span style="font-variant-ligatures: no-common-ligatures" class="">) -&gt; </span><span style="font-variant-ligatures: no-common-ligatures; color: #703daa" class="">Error</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div><p style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></p><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">if</span><span style="font-variant-ligatures: no-common-ligatures" class=""> </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> lhs = lhs { </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> lhs } </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">else</span><span style="font-variant-ligatures: no-common-ligatures" class=""> { </span><span style="font-variant-ligatures: no-common-ligatures; color: #ba2da2" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class=""> rhs }</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">}</span></div></div><div class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Feb 9, 2017, at 12:01 PM, Jack Newcombe &lt;<a href="mailto:jack@newcombe.io" class="">jack@newcombe.io</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">While this is nice, it adds ambiguity to the nil-coalescing operator. For example, when using nil-coalescing with a wrapped error value and an unwrapped error value as operands:<div class=""><br class=""></div><div class="">&nbsp; &nbsp; let optionalError: Errors? = nil</div><div class="">&nbsp; &nbsp; let result = optionalError ?? Errors.generic<br class=""></div><div class=""><br class=""></div><div class="">The above will result in an "Ambiguous use of operator" error. Even if you were to somehow constrain the first argument to arguments of non-error types, it would still make the operator incongruous.</div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Thu, Feb 9, 2017 at 7:08 PM, Hooman Mehr <span dir="ltr" class="">&lt;<a href="mailto:hooman@mac.com" target="_blank" class="">hooman@mac.com</a>&gt;</span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" class="">I think the best solution is overloading the existing ?? operator. It is very easy to do:<div class=""><br class=""></div><div class=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">func</span><span style="font-variant-ligatures:no-common-ligatures" class=""> ??&lt;T,U: </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa" class="">Error</span><span style="font-variant-ligatures:no-common-ligatures" class="">&gt;(lhs: </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="">T</span><span style="font-variant-ligatures:no-common-ligatures" class="">?, rhs: </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="">U</span><span style="font-variant-ligatures:no-common-ligatures" class="">) </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">throws</span><span style="font-variant-ligatures:no-common-ligatures" class=""> -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187" class="">T</span><span style="font-variant-ligatures:no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp;&nbsp; &nbsp;</span><br class="m_-5169000833112052799webkit-block-placeholder"></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">if</span><span style="font-variant-ligatures:no-common-ligatures" class=""> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> lhs = lhs { </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">return</span><span style="font-variant-ligatures:no-common-ligatures" class=""> lhs } </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">else</span><span style="font-variant-ligatures:no-common-ligatures" class=""> { </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">throw</span><span style="font-variant-ligatures:no-common-ligatures" class=""> rhs }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">}</span></div></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="">then you can say:</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=""><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">do</span><span style="font-variant-ligatures:no-common-ligatures" class=""> {</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp;&nbsp; &nbsp;</span><br class="m_-5169000833112052799webkit-block-placeholder"></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">let</span><span style="font-variant-ligatures:no-common-ligatures" class=""> y = </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">try</span><span style="font-variant-ligatures:no-common-ligatures" class=""> x ?? myError</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">&nbsp;&nbsp; &nbsp;</span><br class="m_-5169000833112052799webkit-block-placeholder"></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">} </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2" class="">catch</span><span style="font-variant-ligatures:no-common-ligatures" class=""> ...</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="">It might even make sense to add to the standard library.</span></div></span></div><div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class=""><div class="h5"><div class="">On Feb 9, 2017, at 12:04 AM, Jack Newcombe via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="m_-5169000833112052799Apple-interchange-newline"></div></div><div class=""><div class=""><div class=""><div class="h5">This can actually be accomplished now using a closure:<br class=""><br class=""><span class="m_-5169000833112052799Apple-tab-span" style="white-space:pre-wrap">        </span>let value = optionalValue ?? { throw CustomError.failure }()<br class=""><br class="">However, this adds a layer of indirection that I’m not keen on and lacks the readability and maintainability of a well-defined operator.<br class=""><br class="">The problem with changing the nil-coalescing operator is that it means allowing the second operand to be a statement rather than an expression, which I assume would be seen as an unacceptable.<br class=""><br class=""><blockquote type="cite" class="">On 9 Feb 2017, at 07:56, Brent Royal-Gordon &lt;<a href="mailto:brent@architechies.com" target="_blank" class="">brent@architechies.com</a>&gt; wrote:<br class=""><br class=""><blockquote type="cite" class="">On Feb 8, 2017, at 12:00 PM, Jack Newcombe via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""><br class="">I propose the introduction of a nil-rejection operator (represented here as !!) as a complement to the above operators.<br class="">.<br class="">This operator should allow an equivalent behaviour to the forced unwrapping of a variable, but with the provision of an error to throw in place of throwing a fatal error.<br class=""><br class="">- value !! Error :<br class=""><span class="m_-5169000833112052799Apple-tab-span" style="white-space:pre-wrap">        </span>if value is nil, throw non-fatal error<br class=""><span class="m_-5169000833112052799Apple-tab-span" style="white-space:pre-wrap">        </span>if value is not nil, return value<br class=""><br class="">Example of how this syntax might work (Where CustomError: Error):<br class=""><br class=""><span class="m_-5169000833112052799Apple-tab-span" style="white-space:pre-wrap">        </span>let value = try optionalValue !! CustomError.failure<br class=""></blockquote><br class="">Rather than invent a new operator, I'd prefer to make `throw` an expression rather than a statement. Then you could write:<br class=""><br class=""><span class="m_-5169000833112052799Apple-tab-span" style="white-space:pre-wrap">        </span>let value = optionalValue ?? throw CustomError.Failure<br class=""><br class="">One issue here would be figuring out the proper return type for `throw`. Although if `Never` were a subtype-of-all-types, that would of course work. :^)<br class=""><br class="">-- <br class="">Brent Royal-Gordon<br class="">Architechies<br class=""><br class=""></blockquote><br class=""></div></div><span class="">______________________________<wbr class="">_________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-<wbr class="">evolution</a><br class=""></span></div></div></blockquote></div><br class=""></div></div></blockquote></div><br class=""></div>
</div></blockquote></div><br class=""></div></body></html>