<div dir="ltr"><div class="markdown-here-wrapper" style=""><p style="margin:0px 0px 1.2em!important">I understand how parametric polymorphism works <em>in Haskell</em>. But we talk about Swift, and there <em>is</em> a way to get an instance of <code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline">E</code>. I’ll explain it another way:</p>
<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><code class="hljs language-swift" style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-radius:3px;display:inline;white-space:pre;overflow:auto;border-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block!important;display:block;overflow-x:auto;padding:0.5em;color:rgb(51,51,51);background:rgb(248,248,248)"><span class="hljs-func"><span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">func</span> <span class="hljs-title" style="color:rgb(153,0,0);font-weight:bold">bypassRethrows</span><span class="hljs-generics">&lt;E: Error&gt;</span><span class="hljs-params">(<span class="hljs-number" style="color:rgb(0,128,128)">_</span> f: <span class="hljs-params">()</span> throws<span class="hljs-params">(E)</span> -&gt; <span class="hljs-params">()</span>)</span> <span class="hljs-title" style="color:rgb(153,0,0);font-weight:bold">throws</span><span class="hljs-params">(E)</span> </span>{
    <span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">let</span> error: <span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">Error</span> = <span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">MyError</span>()  <span class="hljs-comment" style="color:rgb(153,153,136);font-style:italic">// create an instance of `MyError`</span>
    <span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">if</span> <span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">MyError</span>.<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">self</span> <span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">is</span> <span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">E</span>.<span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">Type</span> {   <span class="hljs-comment" style="color:rgb(153,153,136);font-style:italic">// in case `E` happens to be `MyError`</span>
        <span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">let</span> e: <span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">E</span> = error <span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">as</span>! <span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">E</span>    <span class="hljs-comment" style="color:rgb(153,153,136);font-style:italic">// then we&#39;ve actually created an instance of `E`, and we can downcast safely</span>
        throw e                   <span class="hljs-comment" style="color:rgb(153,153,136);font-style:italic">// voila, acquired an instance of `E`</span>
    }
}

<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">let</span> f: () throws <span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">MyError</span> -&gt; () = { }
try bypassRethrows(f)             <span class="hljs-comment" style="color:rgb(153,153,136);font-style:italic">// actually throws `MyError`, without ever calling `f`</span>
</code></pre>
<p style="margin:0px 0px 1.2em!important">What line here seems impossible?</p>
<p style="margin:0px 0px 1.2em!important">2017-02-22 18:39 GMT+03:00 Matthew Johnson &lt;<a href="mailto:matthew@anandabits.com" target="_blank">matthew@anandabits.com</a>&gt;:</p>
<p style="margin:0px 0px 1.2em!important"></p><div class="markdown-here-exclude"><p></p><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"><div><div>No because there are many types that conform to `Error` but are not `E` and the signature says you only throw `E`.  You have no way to get an instance of `E` except by catching one thrown by `f` because `Error` does not have any initializers and you don’t have any visibility to anything that provides an `E` in any way except when `f` throws.</div><div><br></div><div>I am hoping to have time to write up my analysis of generic rethrowing functions later today.  There are some very interesting tradeoffs we need to make.</div></div></div></blockquote><p></p></div><p style="margin:0px 0px 1.2em!important"></p>
<div title="MDH:SSB1bmRlcnN0YW5kIGhvdyBwYXJhbWV0cmljIHBvbHltb3JwaGlzbSB3b3JrcyAqaW4gSGFza2Vs
bCouIEJ1dCB3ZSB0YWxrIGFib3V0IFN3aWZ0LCBhbmQgdGhlcmUgKmlzKiBhIHdheSB0byBnZXQg
YW4gaW5zdGFuY2Ugb2YgYEVgLiBJJ2xsIGV4cGxhaW4gaXQgYW5vdGhlciB3YXk6PGRpdj48YnI+
PC9kaXY+PGRpdj5gYGBzd2lmdDwvZGl2PjxkaXY+ZnVuYyBieXBhc3NSZXRocm93cyZsdDtFOiBF
cnJvciZndDsoXyBmOiAoKSB0aHJvd3MoRSkgLSZndDsgKCkpIHRocm93cyhFKSB7PC9kaXY+PGRp
dj4mbmJzcDsgJm5ic3A7IGxldCBlcnJvcjogRXJyb3IgPSBNeUVycm9yKCkgJm5ic3A7Ly8gY3Jl
YXRlIGFuIGluc3RhbmNlIG9mIGBNeUVycm9yYDwvZGl2PjxkaXY+Jm5ic3A7ICZuYnNwOyBpZiBN
eUVycm9yLnNlbGYgaXMgRS5UeXBlIHsgJm5ic3A7IC8vIGluIGNhc2UgYEVgIGhhcHBlbnMgdG8g
YmUgYE15RXJyb3JgPC9kaXY+PGRpdj4mbmJzcDsgJm5ic3A7ICZuYnNwOyAmbmJzcDsgbGV0IGU6
IEUgPSBlcnJvciBhcyEgRSAmbmJzcDsgJm5ic3A7Ly8gdGhlbiB3ZSd2ZSBhY3R1YWxseSBjcmVh
dGVkIGFuIGluc3RhbmNlIG9mIGBFYCwgYW5kIHdlIGNhbiBkb3duY2FzdCBzYWZlbHk8L2Rpdj48
ZGl2PiZuYnNwOyAmbmJzcDsgJm5ic3A7ICZuYnNwOyB0aHJvdyBlICZuYnNwOyAmbmJzcDsgJm5i
c3A7ICZuYnNwOyAmbmJzcDs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