<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 picked !! because it seemed to follow somewhat naturally from force-unwrap and nil-coalescing; however, that does depend on how you interpret the syntax of the operator. <div class=""><br class=""></div><div class="">I interpreted ?? to figuratively mean “? with a default result instead of nil”, and so derived !! As figuratively meaning “! with default error instead of fatal error”.<div class=""><br class=""></div><div class="">Regardless, I’m definitely open to alternatives, although can’t think of anything more appropriate offhand.<div class=""><br class=""></div><div class=""><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On 8 Feb 2017, at 20:20, Jean-Daniel <<a href="mailto:mailing@xenonium.com" class="">mailing@xenonium.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">While I find the concept interesting, I give a big -1 for using the ‘!’ operator for something else that fatal error.<div class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">Le 8 févr. 2017 à 21:00, Jack Newcombe via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> a écrit :</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi all,<div class=""><br class=""></div><div class="">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=""><br class=""></div><div class="">Take the following outcomes for interacting with an optional using existing operators (!, ?, ??). The outcomes of using these are as follows:</div><div class=""><br class=""></div><div class="">- value? : </div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>if value is nil, do nothing and return nil</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </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="">- value! : </div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>if value is nil, throw.a fatal error. </div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </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="">- value ?? default :</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>if value is nil, return default</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>if value is not nil, return value</div><div class=""><br class=""></div><div class="">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=""><br class=""></div><div class="">I propose the introduction of a nil-rejection operator (represented here as !!) as a complement to the above operators.</div><div class="">.</div><div 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.</div><div class=""><br class=""></div><div class="">- value !! Error :</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>if value is nil, throw non-fatal error</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>if value is not nil, return value</div><div class=""><br class=""></div><div class="">Example of how this syntax might work (Where CustomError: Error):</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let value = try optionalValue !! CustomError.failure</div><div class=""><br class=""></div><div class="">It is possible to implement this in Swift 3 with the following declaration:</div><div class=""><br class=""></div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>infix operator !! : NilCoalescingPrecedence<br class=""><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>func !!<UnwrappedType: Any, ErrorType: Error>(lhs: Optional<UnwrappedType>, rhs: ErrorType) throws -> UnwrappedType {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> guard let unwrappedValue = lhs else {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> throw rhs<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> }<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> return unwrappedValue<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div class=""><br class=""></div><div class="">I’ve added further examples including composition with the nil-coalescence operator here: </div><div class=""><a href="https://gist.github.com/jnewc/304bdd2d330131ddb8a1e615ee560d1d" class="">https://gist.github.com/jnewc/304bdd2d330131ddb8a1e615ee560d1d</a></div><div class=""><br class=""></div><div class="">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=""><br class=""></div><div class="">Best regards,</div><div class=""><br class=""></div><div class="">Jack</div><div class=""><br class=""></div></div>_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></blockquote></div><br class=""></div></div></div></blockquote></div><br class=""></div></div></div></div></body></html>