<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=""><div><blockquote type="cite" class=""><div class="">On Feb 16, 2017, at 9:45 PM, Jon Shier via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><div class=""><div 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&nbsp;Optional&nbsp;{<br class="">&nbsp; &nbsp;&nbsp;<br class="">&nbsp; &nbsp;&nbsp;func&nbsp;deoptionalize()&nbsp;throws&nbsp;-&gt;&nbsp;Wrapped&nbsp;{<br class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;switch&nbsp;self&nbsp;{<br class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;case&nbsp;.some(let wrapped):&nbsp;return&nbsp;wrapped<br class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;case&nbsp;.none:&nbsp;throw&nbsp;"\(self.self) was nil."<br class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}<br class="">&nbsp; &nbsp;&nbsp;}<br class="">&nbsp; &nbsp;&nbsp;<br class="">}<br class=""><br class="">extension&nbsp;String:&nbsp;LocalizedError&nbsp;{<br class="">&nbsp; &nbsp;&nbsp;<br class="">&nbsp; &nbsp;&nbsp;public&nbsp;var&nbsp;localizedDescription:&nbsp;String&nbsp;{<br class="">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;self<br class="">&nbsp; &nbsp;&nbsp;}<br class="">&nbsp; &nbsp;&nbsp;<br class="">}</div><div class=""><br class=""></div><div class="">It’s probably a bad idea to make String an Error, but it worked.</div></div></div></blockquote><div><br class=""></div><div>Yeah, this seems reasonable (although you might consider making some earlier part throw the error instead of returning an optional?) except for the completely unnecessary use of String as the error type. &nbsp;Just make a new error enum with one case; it'll be much easier to recognize it if you need to.</div><div><br class=""></div><div>Also, I'm pretty sure that that string is always going to expand to "nil was nil."</div><div><br class=""></div><div>John.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">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 class=""><blockquote type="cite" class=""><div class="">On Feb 9, 2017, at 5:35 PM, Rob Mayoff via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; 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="">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</a>&gt;</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="">&nbsp; &nbsp; func throwMe&lt;R&gt;() throws -&gt; 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="">&nbsp;</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=""><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>_______________________________________________<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=""></body></html>