<div dir="ltr">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><br></div><div>    let optionalError: Errors? = nil</div><div>    let result = optionalError ?? Errors.generic<br></div><div><br></div><div>The above will result in an &quot;Ambiguous use of operator&quot; 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><div class="gmail_quote">On Thu, Feb 9, 2017 at 7:08 PM, Hooman Mehr <span dir="ltr">&lt;<a href="mailto:hooman@mac.com" target="_blank">hooman@mac.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">I think the best solution is overloading the existing ?? operator. It is very easy to do:<div><br></div><div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">func</span><span style="font-variant-ligatures:no-common-ligatures"> ??&lt;T,U: </span><span style="font-variant-ligatures:no-common-ligatures;color:#703daa">Error</span><span style="font-variant-ligatures:no-common-ligatures">&gt;(lhs: </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">T</span><span style="font-variant-ligatures:no-common-ligatures">?, rhs: </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">U</span><span style="font-variant-ligatures:no-common-ligatures">) </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">throws</span><span style="font-variant-ligatures:no-common-ligatures"> -&gt; </span><span style="font-variant-ligatures:no-common-ligatures;color:#4f8187">T</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures">    </span><br class="m_-5169000833112052799webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">if</span><span style="font-variant-ligatures:no-common-ligatures"> </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">let</span><span style="font-variant-ligatures:no-common-ligatures"> lhs = lhs { </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">return</span><span style="font-variant-ligatures:no-common-ligatures"> lhs } </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">else</span><span style="font-variant-ligatures:no-common-ligatures"> { </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">throw</span><span style="font-variant-ligatures:no-common-ligatures"> rhs }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">}</span></div></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div><span style="font-variant-ligatures:no-common-ligatures">then you can say:</span></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div><span style="font-variant-ligatures:no-common-ligatures"><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">do</span><span style="font-variant-ligatures:no-common-ligatures"> {</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures">    </span><br class="m_-5169000833112052799webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">    </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">let</span><span style="font-variant-ligatures:no-common-ligatures"> y = </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">try</span><span style="font-variant-ligatures:no-common-ligatures"> x ?? myError</span></div><p style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures">    </span><br class="m_-5169000833112052799webkit-block-placeholder"></p><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span style="font-variant-ligatures:no-common-ligatures">} </span><span style="font-variant-ligatures:no-common-ligatures;color:#ba2da2">catch</span><span style="font-variant-ligatures:no-common-ligatures"> ...</span></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div><span style="font-variant-ligatures:no-common-ligatures">It might even make sense to add to the standard library.</span></div></span></div><div><br><div><blockquote type="cite"><div><div class="h5"><div>On Feb 9, 2017, at 12:04 AM, Jack Newcombe via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:</div><br class="m_-5169000833112052799Apple-interchange-newline"></div></div><div><div><div><div class="h5">This can actually be accomplished now using a closure:<br><br><span class="m_-5169000833112052799Apple-tab-span" style="white-space:pre-wrap">        </span>let value = optionalValue ?? { throw CustomError.failure }()<br><br>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><br>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><br><blockquote type="cite">On 9 Feb 2017, at 07:56, Brent Royal-Gordon &lt;<a href="mailto:brent@architechies.com" target="_blank">brent@architechies.com</a>&gt; wrote:<br><br><blockquote type="cite">On Feb 8, 2017, at 12:00 PM, Jack Newcombe via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br><br>I propose the introduction of a nil-rejection operator (represented here as !!) as a complement to the above operators.<br>.<br>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><br>- value !! Error :<br><span class="m_-5169000833112052799Apple-tab-span" style="white-space:pre-wrap">        </span>if value is nil, throw non-fatal error<br><span class="m_-5169000833112052799Apple-tab-span" style="white-space:pre-wrap">        </span>if value is not nil, return value<br><br>Example of how this syntax might work (Where CustomError: Error):<br><br><span class="m_-5169000833112052799Apple-tab-span" style="white-space:pre-wrap">        </span>let value = try optionalValue !! CustomError.failure<br></blockquote><br>Rather than invent a new operator, I&#39;d prefer to make `throw` an expression rather than a statement. Then you could write:<br><br><span class="m_-5169000833112052799Apple-tab-span" style="white-space:pre-wrap">        </span>let value = optionalValue ?? throw CustomError.Failure<br><br>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><br>-- <br>Brent Royal-Gordon<br>Architechies<br><br></blockquote><br></div></div><span class="">______________________________<wbr>_________________<br>swift-evolution mailing list<br><a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/<wbr>mailman/listinfo/swift-<wbr>evolution</a><br></span></div></div></blockquote></div><br></div></div></blockquote></div><br></div>