<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="">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></body></html>