<div dir="ltr">Thanks Howard, it's a good workaround. <div><br></div><div>Do you think it would be better to be part of swift language feature? I wish swift team could consider this :)</div></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Jul 6, 2017 at 11:04 AM Howard Lovatt <<a href="mailto:howard.lovatt@gmail.com">howard.lovatt@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">You could use a result type, e.g.:<div><br></div><div><a href="https://github.com/antitypical/Result" target="_blank">https://github.com/antitypical/Result</a><br></div><div><br></div><div>Or roll your own result type.</div><div><br></div><div>I think the reason that Swift doesn't support what you want is because of rethrows. When you declare a method as rethrows it can throw anything because the closure it is re-throwing can throw anything. They could have fully typed the rethrows but obviously decided that was not worth it. At present rethrows is light weight; the compiler generates two versions of the method, one that throws and one that doesn't. If it was typed then it would be like a generic method and a version of the method would be required for each type combination that was actually used.</div></div><div class="gmail_extra"></div><div class="gmail_extra"><br clear="all"><div><div class="m_6313932426561308567gmail_signature" data-smartmail="gmail_signature"> -- Howard.<br></div></div>
<br></div><div class="gmail_extra"><div class="gmail_quote">On 6 July 2017 at 10:38, Tim Wang via swift-users <span dir="ltr"><<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>></span> wrote:<br></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi Swifters,<div><br></div><div>I am wondering if it is possible to specify error types thrown in a protocol method. By allowing us to do it and letting the compiler check all the implementations of the protocol to make sure only they would only throw the specified error types, we only need to catch our error types when calling these methods. </div><div><br></div><div>
For the code below:<br><br>
<p class="m_6313932426561308567m_-8446920124172038182inbox-inbox-p1"><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s1">enum</span> MyError: <span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s2">Error</span> {</p>
<p class="m_6313932426561308567m_-8446920124172038182inbox-inbox-p1"><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-Apple-converted-space"> </span><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s1">case</span> justError</p>
<p class="m_6313932426561308567m_-8446920124172038182inbox-inbox-p1">}</p>
<p class="m_6313932426561308567m_-8446920124172038182inbox-inbox-p1"><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s1">protocol</span> MethodWillThrow {</p>
<p class="m_6313932426561308567m_-8446920124172038182inbox-inbox-p1"><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-Apple-converted-space"> </span><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s1">func</span> testMethod() <span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s1">throws</span> MyError</p>
<p class="m_6313932426561308567m_-8446920124172038182inbox-inbox-p1">}</p>
<p class="m_6313932426561308567m_-8446920124172038182inbox-inbox-p2"><br></p>
<p class="m_6313932426561308567m_-8446920124172038182inbox-inbox-p3"><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s1">extension</span><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s3"> </span>MethodThrow<span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s3"> {</span></p>
<p class="m_6313932426561308567m_-8446920124172038182inbox-inbox-p1"><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-Apple-converted-space"> </span><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s1">func</span> testMethod() <span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s1">throws</span> {</p>
<p class="m_6313932426561308567m_-8446920124172038182inbox-inbox-p3"><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s3"><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-Apple-converted-space"> </span></span><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s1">throw</span><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s3"> </span>MyError<span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s3">.</span>justError</p>
<p class="m_6313932426561308567m_-8446920124172038182inbox-inbox-p1"><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-Apple-converted-space"> </span>}</p>
<p class="m_6313932426561308567m_-8446920124172038182inbox-inbox-p1">}</p>
<p class="m_6313932426561308567m_-8446920124172038182inbox-inbox-p1"><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s1">class</span> TestClass: <span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s4">MethodThrow</span> {</p>
<p class="m_6313932426561308567m_-8446920124172038182inbox-inbox-p1"><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-Apple-converted-space"> </span><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s1">func</span> testMethod() <span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s1">throws</span> {</p>
<p class="m_6313932426561308567m_-8446920124172038182inbox-inbox-p3"><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s3"><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-Apple-converted-space"> </span></span><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s1">throw</span><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s3"> </span>MyError<span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s3">.</span>justError</p>
<p class="m_6313932426561308567m_-8446920124172038182inbox-inbox-p1"><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-Apple-converted-space"> </span>}</p>
<p class="m_6313932426561308567m_-8446920124172038182inbox-inbox-p1"><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-Apple-converted-space"> </span><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s1">func</span> anotherMethod() {</p>
<p class="m_6313932426561308567m_-8446920124172038182inbox-inbox-p1"><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-Apple-converted-space"> </span><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s1">do</span> {</p>
<p class="m_6313932426561308567m_-8446920124172038182inbox-inbox-p1"><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-Apple-converted-space"> </span><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s1">try</span> <span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s4">testMethod</span>()</p>
<p class="m_6313932426561308567m_-8446920124172038182inbox-inbox-p1"><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-Apple-converted-space"> </span>} <span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s1">catch</span> MyError.<span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s4">justError</span> {</p>
<p class="m_6313932426561308567m_-8446920124172038182inbox-inbox-p1"><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-Apple-converted-space"> </span><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s2">print</span>(<span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s5">"my error"</span>)</p>
<p class="m_6313932426561308567m_-8446920124172038182inbox-inbox-p1"><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-Apple-converted-space"> </span>} <b><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s1">catch</span> {</b></p>
<p class="m_6313932426561308567m_-8446920124172038182inbox-inbox-p1"><b><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-Apple-converted-space"> </span><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s2">print</span>(<span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-s5">"other error"</span>)</b></p>
<p class="m_6313932426561308567m_-8446920124172038182inbox-inbox-p1"><b><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-Apple-converted-space"> </span>}</b></p>
<p class="m_6313932426561308567m_-8446920124172038182inbox-inbox-p1"><span class="m_6313932426561308567m_-8446920124172038182inbox-inbox-Apple-converted-space"> </span>}</p>
<p class="m_6313932426561308567m_-8446920124172038182inbox-inbox-p1">}</p><p class="m_6313932426561308567m_-8446920124172038182inbox-inbox-p1">Now we need add this extra default catch to make it compile and work and I really want to remove this catch.</p><p class="m_6313932426561308567m_-8446920124172038182inbox-inbox-p1">Please let me know if there is a way to do it and thanks for help in advance.<span class="m_6313932426561308567HOEnZb"><font color="#888888"><br></font></span></p><span class="m_6313932426561308567HOEnZb"><font color="#888888"><p class="m_6313932426561308567m_-8446920124172038182inbox-inbox-p1">Tim Wang</p></font></span></div></div>
<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
<br></blockquote></div></div></blockquote></div>