<div dir="ltr"><div class="markdown-here-wrapper" style=""><blockquote style="margin:1.2em 0px;border-left-width:4px;border-left-style:solid;border-left-color:rgb(221,221,221);padding:0px 1em;color:rgb(119,119,119);quotes:none">
<p style="margin:0px 0px 1.2em!important">You’ve got two things wrong there:</p>
<ol style="margin:1.2em 0px;padding-left:2em">
<li style="margin:0.5em 0px"><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);border-radius:3px;display:inline;background-color:rgb(248,248,248)">Derived: Base</code> otherwise you cannot cast at all.</li>
<li style="margin:0.5em 0px"><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);border-radius:3px;display:inline;background-color:rgb(248,248,248)">w === x //=&gt; FALSE</code></li>
</ol>
<p style="margin:0px 0px 1.2em!important">The reason why this does not work is because it’s impossible to cast a type to an different type with the help of an initializer. As I cleaned up the current implementation I though about replacing <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);border-radius:3px;display:inline;background-color:rgb(248,248,248)">init?(casting: Type&lt;T&gt;?)</code> with something like this:</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);border-radius:3px;display:inline;background-color:rgb(248,248,248);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)">public <span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">static</span> <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">cast</span><span class="hljs-generics">&lt;U&gt;</span><span class="hljs-params">(from optionalType: <span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">Type</span>&lt;U&gt;?)</span> -&gt; <span class="hljs-title" style="color:rgb(153,0,0);font-weight:bold">Type</span><span class="hljs-generics">&lt;T&gt;</span>? </span>{

    guard <span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">let</span> otherType = optionalType <span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">else</span> { <span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">return</span> <span class="hljs-built_in" style="color:rgb(0,134,179)">nil</span> }

    <span class="hljs-comment" style="color:rgb(153,153,136);font-style:italic">// Check if we can up- or downcast the metatype from `otherType` to `Metatype&lt;T&gt;`</span>

    <span class="hljs-comment" style="color:rgb(153,153,136);font-style:italic">// Bug: SR-2085</span>
    <span class="hljs-comment" style="color:rgb(153,153,136);font-style:italic">// Workaround: Check explicitly if `T` is `Any`</span>
    <span class="hljs-comment" style="color:rgb(153,153,136);font-style:italic">//</span>
    <span class="hljs-comment" style="color:rgb(153,153,136);font-style:italic">// let isTAny = _uniqueIdentifierOf(Any.metatype) == _uniqueIdentifierOf(T.metatype)</span>
    <span class="hljs-comment" style="color:rgb(153,153,136);font-style:italic">// guard isTAny || otherType._metatype is Metatype&lt;T&gt; else {</span>
    <span class="hljs-comment" style="color:rgb(153,153,136);font-style:italic">//      return nil</span>
    <span class="hljs-comment" style="color:rgb(153,153,136);font-style:italic">// }</span>

    guard otherType._metatype <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">Metatype</span>&lt;<span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">T</span>&gt; <span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">else</span> {
        <span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">return</span> <span class="hljs-built_in" style="color:rgb(0,134,179)">nil</span>
    }
    <span class="hljs-comment" style="color:rgb(153,153,136);font-style:italic">// `T` implicitly converted to `Type&lt;T&gt;()`</span>
    <span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">return</span> unsafeBitCast(otherType, to: <span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">T</span>)
}
</code></pre>
</blockquote>
<p style="margin:0px 0px 1.2em!important">Brilliant! But I disagree with <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);border-radius:3px;display:inline;background-color:rgb(248,248,248)">from</code> label. “Cast from type” means that we somehow use <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);border-radius:3px;display:inline;background-color:rgb(248,248,248)">Type&lt;U&gt;</code> statically. But we need to cast <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);border-radius:3px;display:inline;background-color:rgb(248,248,248)">optionalType</code> itself to <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);border-radius:3px;display:inline;background-color:rgb(248,248,248)">Type&lt;T&gt;</code>. I suggest to use <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);border-radius:3px;display:inline;background-color:rgb(248,248,248)">cast(_:)</code> signature.</p>
<p style="margin:0px 0px 1.2em!important">Next, I’d like to remind you of my <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);border-radius:3px;display:inline;background-color:rgb(248,248,248)">size</code>, <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);border-radius:3px;display:inline;background-color:rgb(248,248,248)">stride</code>, <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);border-radius:3px;display:inline;background-color:rgb(248,248,248)">align</code> concerns. Consider this example:</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);border-radius:3px;display:inline;background-color:rgb(248,248,248);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-keyword" style="color:rgb(51,51,51);font-weight:bold">let</span> x = <span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">Type</span>&lt;<span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">Int</span>&gt;()
<span class="hljs-keyword" style="color:rgb(51,51,51);font-weight:bold">let</span> y = <span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">Type</span>&lt;<span class="hljs-type" style="color:rgb(68,85,136);font-weight:bold">CustomStringConvertible</span>&gt;(casting: x)!
x.size  <span class="hljs-comment" style="color:rgb(153,153,136);font-style:italic">//=&gt; 8</span>
y.size  <span class="hljs-comment" style="color:rgb(153,153,136);font-style:italic">//=&gt; 8 or 40?</span>
</code></pre>
<p style="margin:0px 0px 1.2em!important">I think that <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);border-radius:3px;display:inline;background-color:rgb(248,248,248)">y.size</code> should be 8, because we are dynamically checking size of particular instance <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);border-radius:3px;display:inline;background-color:rgb(248,248,248)">y</code>, which refers to specific subtype 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);border-radius:3px;display:inline;background-color:rgb(248,248,248)">CustomStringConvertible</code>. We should look into corresponding witness table or whatever, and get the size at runtime. In the future, when we add more reflection features, they will also collect information about particular type instances.</p>
<div title="MDH:PGRpdiBjbGFzcz0iZ21haWxfZXh0cmEiPjxkaXYgY2xhc3M9ImdtYWlsX3F1b3RlIj4mZ3Q7IFlv
deKAmXZlIGdvdCB0d28gdGhpbmdzIHdyb25nIHRoZXJlOjxicj48L2Rpdj48ZGl2IGNsYXNzPSJn
bWFpbF9xdW90ZSI+PGRpdiBjbGFzcz0iZ21haWxfcXVvdGUiPiZndDs8L2Rpdj48ZGl2IGNsYXNz
PSJnbWFpbF9xdW90ZSI+Jmd0OzEuIGBEZXJpdmVkOiBCYXNlYCBvdGhlcndpc2UgeW91IGNhbm5v
dCBjYXN0IGF0IGFsbC48L2Rpdj48ZGl2IGNsYXNzPSJnbWFpbF9xdW90ZSI+Jmd0OzIuIGB3ID09
PSB4IC8vPSZndDsgRkFMU0VgPC9kaXY+PGRpdiBjbGFzcz0iZ21haWxfcXVvdGUiPiZndDs8L2Rp
dj48ZGl2IGNsYXNzPSJnbWFpbF9xdW90ZSI+Jmd0O1RoZSByZWFzb24gd2h5IHRoaXMgZG9lcyBu
b3Qgd29yayBpcyBiZWNhdXNlIGl04oCZcyBpbXBvc3NpYmxlIHRvIGNhc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