<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 created this set of extensions so I could more easily deal with optionals in a context where I was already potentially throwing errors.<div class=""><br class=""></div><div class="">extension Optional {<br class=""> <br class=""> func deoptionalize() throws -> Wrapped {<br class=""> switch self {<br class=""> case .some(let wrapped): return wrapped<br class=""> case .none: throw "\(self.self) was nil."<br class=""> }<br class=""> }<br class=""> <br class="">}<br class=""><br class="">extension String: LocalizedError {<br class=""> <br class=""> public var localizedDescription: String {<br class=""> return self<br class=""> }<br class=""> <br class="">}</div><div class=""><br class=""></div><div class="">It’s probably a bad idea to make String an Error, but it worked. It seems to me like there should be something more convenient for dealing with Optionals in contexts where unwrapping them is awkward.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Jon</div><div class=""><br class=""></div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Feb 9, 2017, at 5:35 PM, Rob Mayoff via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="">On Thu, Feb 9, 2017 at 2:25 PM, Haravikk via swift-evolution <span dir="ltr" class=""><<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>></span> wrote:<br class=""></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word" class=""><div class="">I wonder if an alternative to the original proposal might be to allow throw on the right hand side? So you could do:</div><div class=""><br class=""></div><div class=""><font face="Monaco" class=""><span class="gmail-m_2320481702251049346Apple-tab-span" style="white-space:pre-wrap">        </span>let y = x ?? throw myError</font></div><div class=""><br class=""></div><div class=""></div></div></blockquote><div class=""><br class=""></div><div class="">You can do this today:</div><div class=""><br class=""></div><div class="">extension Error {</div><div class=""> func throwMe<R>() throws -> R { throw self }</div><div class="">}</div><div class=""><br class=""></div><div class="">let y = try x ?? MyError().throwMe()<br class=""></div><div class=""> </div></div></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="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></body></html>