<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="">It’s absolutely true that this is syntactic sugar, but then so is nil-coalescing where "x ?? y” is syntactic sugar for “x != nil ? x : y”.&nbsp;<div class=""><br class=""></div><div class="">You can also similarly recreate the nil-coalescing operator in Swift yourself, so I’m not sure that’s a strong argument for any operator being or not being in the standard library.</div><div class=""><br class=""></div><div class=""><div class=""><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 8 Feb 2017, at 20:29, Xiaodi Wu &lt;<a href="mailto:xiaodi.wu@gmail.com" class="">xiaodi.wu@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class="">This seems to boil down to sugar where `guard let foo = ... else { throw ... }` is spelled `let foo = try ... !! ...`.<br class=""><br class="">While the analysis is interesting I don't see that this is an obvious win sufficient for the standard library. As you show it's possible to create for yourself.<br class=""><div class="gmail_quote"><div dir="ltr" class="">On Wed, Feb 8, 2017 at 14:20 Jean-Daniel via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class=""></div><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="gmail_msg">While I find the concept interesting, I give a big -1 for using the ‘!’ operator for something else that fatal error.</div><div style="word-wrap:break-word" class="gmail_msg"><div class="gmail_msg"><br class="gmail_msg"><div class="gmail_msg"><blockquote type="cite" class="gmail_msg"><div class="gmail_msg">Le 8 févr. 2017 à 21:00, Jack Newcombe via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a>&gt; a écrit :</div><br class="m_3149029241138271543Apple-interchange-newline gmail_msg"><div class="gmail_msg"><div style="word-wrap:break-word" class="gmail_msg">Hi all,<div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Currently there are a number of different operators for dealing with optionals that cover most of the use cases. However, I think I’ve identified a missing complement for the existing operators for optionals.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Take the following outcomes for interacting with an optional using existing operators (!, ?, ??). The outcomes of using these are as follows:</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">- value? :&nbsp;</div><div class="gmail_msg"><span class="gmail_msg m_3149029241138271543Apple-tab-span" style="white-space:pre-wrap">        </span>if value is nil, do nothing and return nil</div><div class="gmail_msg"><span class="gmail_msg m_3149029241138271543Apple-tab-span" style="white-space:pre-wrap">        </span>if value is not nil, complete the chain by evaluating the rest of the expression. Return the result of the expression</div><div class="gmail_msg">- value! :&nbsp;</div><div class="gmail_msg"><span class="gmail_msg m_3149029241138271543Apple-tab-span" style="white-space:pre-wrap">        </span>if value is nil, throw.a fatal error.&nbsp;</div><div class="gmail_msg"><span class="gmail_msg m_3149029241138271543Apple-tab-span" style="white-space:pre-wrap">        </span>If value is not nil, complete the chain by evaluating the rest of the expression. Return the result of the expression</div><div class="gmail_msg">- value ?? default :</div><div class="gmail_msg"><span class="gmail_msg m_3149029241138271543Apple-tab-span" style="white-space:pre-wrap">        </span>if value is nil, return default</div><div class="gmail_msg"><span class="gmail_msg m_3149029241138271543Apple-tab-span" style="white-space:pre-wrap">        </span>if value is not nil, return value</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">It seems to me that, if it is possible to coalesce a nil value with a default value, it should also be possible to reject a nil value a non-fatal error.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">I propose the introduction of a nil-rejection operator (represented here as !!) as a complement to the above operators.</div><div class="gmail_msg">.</div><div class="gmail_msg">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.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">- value !! Error :</div><div class="gmail_msg"><span class="gmail_msg m_3149029241138271543Apple-tab-span" style="white-space:pre-wrap">        </span>if value is nil, throw non-fatal error</div><div class="gmail_msg"><span class="gmail_msg m_3149029241138271543Apple-tab-span" style="white-space:pre-wrap">        </span>if value is not nil, return value</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Example of how this syntax might work (Where CustomError: Error):</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><span class="gmail_msg m_3149029241138271543Apple-tab-span" style="white-space:pre-wrap">        </span>let value = try optionalValue !! CustomError.failure</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">It is possible to implement this in Swift 3 with the following declaration:</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><span class="gmail_msg m_3149029241138271543Apple-tab-span" style="white-space:pre-wrap">        </span>infix&nbsp;operator&nbsp;!! : NilCoalescingPrecedence<br class="gmail_msg"><br class="gmail_msg"><span class="gmail_msg m_3149029241138271543Apple-tab-span" style="white-space:pre-wrap">        </span>func&nbsp;!!&lt;UnwrappedType:&nbsp;Any,&nbsp;ErrorType:&nbsp;Error&gt;(lhs:&nbsp;Optional&lt;UnwrappedType&gt;,&nbsp;rhs:&nbsp;ErrorType)&nbsp;throws&nbsp;-&gt;&nbsp;UnwrappedType {<br class="gmail_msg"><span class="gmail_msg m_3149029241138271543Apple-tab-span" style="white-space:pre-wrap">        </span>&nbsp; &nbsp;&nbsp;guard&nbsp;let&nbsp;unwrappedValue&nbsp;=&nbsp;lhs&nbsp;else&nbsp;{<br class="gmail_msg"><span class="gmail_msg m_3149029241138271543Apple-tab-span" style="white-space:pre-wrap">        </span>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;throw&nbsp;rhs<br class="gmail_msg"><span class="gmail_msg m_3149029241138271543Apple-tab-span" style="white-space:pre-wrap">        </span>&nbsp; &nbsp; }<br class="gmail_msg"><span class="gmail_msg m_3149029241138271543Apple-tab-span" style="white-space:pre-wrap">        </span>&nbsp; &nbsp;&nbsp;return&nbsp;unwrappedValue<br class="gmail_msg"><span class="gmail_msg m_3149029241138271543Apple-tab-span" style="white-space:pre-wrap">        </span>}</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">I’ve added further examples including composition with the nil-coalescence operator here:&nbsp;</div><div class="gmail_msg"><a href="https://gist.github.com/jnewc/304bdd2d330131ddb8a1e615ee560d1d" class="gmail_msg" target="_blank">https://gist.github.com/jnewc/304bdd2d330131ddb8a1e615ee560d1d</a></div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">This would be particularly convenient in cases where a functions expects significant number of optional to contain non-nil values, without the need to repeat non-generic guard-let structures with the same else code-block.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Best regards,</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Jack</div><div class="gmail_msg"><br class="gmail_msg"></div></div>_______________________________________________<br class="gmail_msg">swift-evolution mailing list<br class="gmail_msg"><a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="gmail_msg"><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="gmail_msg"></div></blockquote></div><br class="gmail_msg"></div></div>_______________________________________________<br class="gmail_msg">
swift-evolution mailing list<br class="gmail_msg">
<a href="mailto:swift-evolution@swift.org" class="gmail_msg" target="_blank">swift-evolution@swift.org</a><br class="gmail_msg">
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" class="gmail_msg" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class="gmail_msg">
</blockquote></div>
</div></blockquote></div><br class=""></div></div></div></body></html>