<html><head><style>
body {
        font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
        padding:1em;
        margin:auto;
        background:#fefefe;
}

h1, h2, h3, h4, h5, h6 {
        font-weight: bold;
}

h1 {
        color: #000000;
        font-size: 28pt;
}

h2 {
        border-bottom: 1px solid #CCCCCC;
        color: #000000;
        font-size: 24px;
}

h3 {
        font-size: 18px;
}

h4 {
        font-size: 16px;
}

h5 {
        font-size: 14px;
}

h6 {
        color: #777777;
        background-color: inherit;
        font-size: 14px;
}

hr {
        height: 0.2em;
        border: 0;
        color: #CCCCCC;
        background-color: #CCCCCC;
    display: inherit;
}

p, blockquote, ul, ol, dl, li, table, pre {
        margin: 15px 0;
}

a, a:visited {
        color: #4183C4;
        background-color: inherit;
        text-decoration: none;
}

#message {
        border-radius: 6px;
        border: 1px solid #ccc;
        display:block;
        width:100%;
        height:60px;
        margin:6px 0px;
}

button, #ws {
        font-size: 12 pt;
        padding: 4px 6px;
        border-radius: 5px;
        border: 1px solid #bbb;
        background-color: #eee;
}

code, pre, #ws, #message {
        font-family: Monaco;
        font-size: 10pt;
        border-radius: 3px;
        background-color: #F8F8F8;
        color: inherit;
}

code {
        border: 1px solid #EAEAEA;
        margin: 0 2px;
        padding: 0 5px;
}

pre {
        border: 1px solid #CCCCCC;
        overflow: auto;
        padding: 4px 8px;
}

pre > code {
        border: 0;
        margin: 0;
        padding: 0;
}

#ws { background-color: #f8f8f8; }


.bloop_markdown table {
border-collapse: collapse;  
font-family: Helvetica, arial, freesans, clean, sans-serif;  
color: rgb(51, 51, 51);  
font-size: 15px; line-height: 25px;
padding: 0; }

.bloop_markdown table tr {
border-top: 1px solid #cccccc;
background-color: white;
margin: 0;
padding: 0; }
     
.bloop_markdown table tr:nth-child(2n) {
background-color: #f8f8f8; }

.bloop_markdown table tr th {
font-weight: bold;
border: 1px solid #cccccc;
margin: 0;
padding: 6px 13px; }

.bloop_markdown table tr td {
border: 1px solid #cccccc;
margin: 0;
padding: 6px 13px; }

.bloop_markdown table tr th :first-child, table tr td :first-child {
margin-top: 0; }

.bloop_markdown table tr th :last-child, table tr td :last-child {
margin-bottom: 0; }

.bloop_markdown blockquote{
  border-left: 4px solid #dddddd;
  padding: 0 15px;
  color: #777777; }
  blockquote > :first-child {
    margin-top: 0; }
  blockquote > :last-child {
    margin-bottom: 0; }

code, pre, #ws, #message {
    word-break: normal;
    word-wrap: normal;
}

hr {
    display: inherit;
}

.bloop_markdown :first-child {
    -webkit-margin-before: 0;
}

code, pre, #ws, #message {
    font-family: Menlo, Consolas, Liberation Mono, Courier, monospace;
}


.send { color:#77bb77; }
.server { color:#7799bb; }
.error { color:#AA0000; }</style></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div class="bloop_markdown"><p><a href="https://github.com/DevAndArtist/swift-evolution/blob/rename_t_dot_type/proposals/0126-rename-t-dot-type.md">https://github.com/DevAndArtist/swift-evolution/blob/rename_t_dot_type/proposals/0126-rename-t-dot-type.md</a></p>

<hr>

<h1 id="renamet.type">Rename <code>T.Type</code></h1>

<ul>
<li>Proposal: <a href="0126-refactor-metatypes-repurpose-t-dot-self-and-mirror.md">SE–0126</a></li>
<li>Authors: <a href="https://github.com/DevAndArtist">Adrian Zubarev</a>, <a href="https://github.com/Anton3">Anton Zhilin</a></li>
<li>Status: <strong>Revision</strong></li>
<li>Review manager: <a href="http://github.com/lattner">Chris Lattner</a></li>
<li>Revision: 2</li>
<li>Previous Revisions: <a href="https://github.com/apple/swift-evolution/blob/83707b0879c83dcde778f8163f5768212736fdc2/proposals/0126-refactor-metatypes-repurpose-t-dot-self-and-mirror.md">1</a></li>
</ul>

<h2 id="introduction">Introduction</h2>

<p>This proposal renames the current metatype <code>T.Type</code> notation and the global function from <strong>SE–0096</strong> to match the changes.</p>

<p>Swift-evolution threads: </p>

<ul>
<li><a href="">[Pitch] Rename <code>T.Type</code></a></li>
<li><a href="">[Review] SE–0126: Refactor Metatypes, repurpose T[dot]self and Mirror</a></li>
<li><a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160718/024772.html">[Proposal] Refactor Metatypes, repurpose T[dot]self and Mirror</a></li>
<li><a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160704/023818.html">[Discussion] Seal <code>T.Type</code> into <code>Type&lt;T&gt;</code></a></li>
</ul>

<h2 id="motivation">Motivation</h2>

<p>In Swift metatypes have the following notation: <strong><code>T.Type</code></strong></p>

<p>As already showed in <strong>SE–0096</strong> and <strong>SE–0090</strong> the Swift community strongly is in favor of (re)moving magical intstance or type properties.</p>

<ul>
<li><p><strong>SE–0096</strong> moves <code>instanceOfT.dynamicType</code> to <code>type&lt;T&gt;(of: T) -&gt; T.Type</code>.</p></li>
<li><p><strong>SE–0090</strong> aims to remove <code>.self</code> completely.</p></li>
</ul>

<p>We propose to rename <code>T.Type</code> to a generic-like notation <code>Metatype&lt;T&gt;</code>. To be able to achieve this notation we have to resolve a few issues first.</p>

<h3 id="knownissuesofmetatypes:">Known issues of metatypes:</h3>

<p>Assume this function that checks if an <code>Int</code> type conforms to a specific protocol. This check uses current model of metatypes combined in a generic context:</p>

<pre><code class="swift">func intConforms&lt;T&gt;(to _: T.Type) -&gt; Bool {
   return Int.self is T.Type
}

intConforms(to: CustomStringConvertible.self) //=&gt; false

Int.self is CustomStringConvertible.Type      //=&gt; true
</code></pre>

<blockquote>
<p>[1] When <code>T</code> is a protocol <code>P</code>, <code>T.Type</code> is the metatype of the protocol type itself, <code>P.Protocol</code>. <code>Int.self</code> is not <code>P.self</code>.</p>

<p>[2] There isn’t a way to generically expression <code>P.Type</code> <strong>yet</strong>.</p>

<p>[3] The syntax would have to be changed in the compiler to get something that behaves like <code>.Type</code> today.</p>

<p>Written by Joe Groff: <a href="https://twitter.com/jckarter/status/754420461404958721">[1]</a> <a href="https://twitter.com/jckarter/status/754420624261472256">[2]</a> <a href="https://twitter.com/jckarter/status/754425573762478080">[3]</a></p>
</blockquote>

<p>A possible workaround might look like the example below, but does not allow to decompose <code>P.Type</code>:</p>

<pre><code class="swift">func intConforms&lt;T&gt;(to _: T.Type) -&gt; Bool {
  return Int.self is T
}

intConforms(to: CustomStringConvertible.Type.self) //=&gt; true
</code></pre>

<p>We can extend this issue and find the second problem by checking against the metatype of <code>Any</code>:</p>

<pre><code class="swift">func intConforms&lt;T&gt;(to _: T.Type) -&gt; Bool {
    return Int.self is T
}

intConforms(to: Any.Type.self) //=&gt; true

intConforms(to: Any.self)      //=&gt; true

Int.self is Any.Type           //=&gt; Always true
</code></pre>

<p>When using <code>Any</code> the compiler does not require <code>.Type</code> at all and returns <code>true</code> for both variations.</p>

<p>The third issue will show itself whenever we would try to check protocol relationship with another protocol. Currently there is no way (that we know of) to solve this problem:</p>

<pre><code class="swift">protocol P {}
protocol R : P {}

func rIsSubtype&lt;T&gt;(of _: T.Type) -&gt; Bool {
    return R.self is T
}

rIsSubtype(of: P.Type.self) //=&gt; false

R.self is Any.Type //=&gt; Always true
R.self is P.Type   //=&gt; true
R.self is R.Type   //=&gt; true
</code></pre>

<p>We also believe that this issue is the reason why the current global functions <code>sizeof</code>, <code>strideof</code> and <code>alignof</code> make use of generic <code>&lt;T&gt;(_: T.Type)</code> declaration notation instead of <code>(_: Any.Type)</code>.</p>

<h2 id="proposedsolution">Proposed solution</h2>

<ul>
<li><p>Rename any occurrence of <code>T.Type</code> and <code>T.Protocol</code> to <code>Metatype&lt;T&gt;</code>.</p></li>
<li><p>Revise metatypes internally. </p></li>
<li><p>When <code>T</code> is a protocol, <code>T.self</code> should always return an instance of <code>Metatype&lt;T&gt;</code> (old <code>T.Type</code>) and never a <code>T.Protocol</code>. Furthermore, metatypes should reflect the same type relationship behavior like the actual types themselves. </p></li>
<li><p>To match the correct meaning and usage of the noun ‘Metatype’ from this proposal, we also propose to rename the global function from <strong>SE–0096</strong>:</p>

<ul>
<li>before: <code>public func type&lt;T&gt;(of instance: T) -&gt; T.Type</code></li>
<li>after: <code>public func metatype&lt;T&gt;(of instance: T) -&gt; Metatype&lt;T&gt;</code></li>
</ul></li>
</ul>

<h3 id="examples:">Examples:</h3>

<pre><code class="swift">protocol P {}
protocol R : P {}
class A : P {}
class B : A, R {}

func `is`&lt;T&gt;(metatype: Metatype&lt;Any&gt;, also _: Metatype&lt;T&gt; ) -&gt; Bool {
    return metatype is Metatype&lt;T&gt;
}

`is`(metatype: R.self, also: Any.self) //=&gt; true | Currently: false
`is`(metatype: R.self, also: P.self)   //=&gt; true | Currently: false
`is`(metatype: R.self, also: R.self)   //=&gt; true

`is`(metatype: B.self, also: Any.self) //=&gt; true | Currently: false
`is`(metatype: B.self, also: P.self)   //=&gt; true | Currently: false
`is`(metatype: B.self, also: R.self)   //=&gt; true | Currently: false
`is`(metatype: B.self, also: A.self)   //=&gt; true
`is`(metatype: B.self, also: B.self)   //=&gt; true

func cast&lt;T&gt;(metatype: Metatype&lt;Any&gt;, to _: Metatype&lt;T&gt;) -&gt; Metatype&lt;T&gt;? {
    return metatype as? Metatype&lt;T&gt;
}

cast(metatype: R.self, to: Any.self)     //=&gt; an Optional&lt;Metatype&lt;Any&gt;&gt; | Currently: nil
cast(metatype: R.self, to: P.self)       //=&gt; an Optional&lt;Metatype&lt;P&gt;&gt;   | Currently: nil
cast(metatype: R.self, to: R.self)       //=&gt; an Optional&lt;Metatype&lt;R&gt;&gt;   | Currently: an Optional&lt;R.Protocol&gt;

let anyR: Any.Type = R.self
let r = cast(metatype: anyR, to: R.self) //=&gt; an Optional&lt;Metatype&lt;R&gt;&gt;   | Currently: an Optional&lt;R.Protocol&gt;

cast(metatype: B.self, to: Any.self)     //=&gt; an Optional&lt;Metatype&lt;Any&gt;&gt; | Currently: nil
cast(metatype: B.self, to: P.self)       //=&gt; an Optional&lt;Metatype&lt;P&gt;&gt;   | Currently: nil
cast(metatype: B.self, to: R.self)       //=&gt; an Optional&lt;Metatype&lt;R&gt;&gt;   | Currently: nil
cast(metatype: B.self, to: A.self)       //=&gt; an Optional&lt;Metatype&lt;A&gt;&gt;
cast(metatype: B.self, to: B.self)       //=&gt; an Optional&lt;Metatype&lt;B&gt;&gt;

let pB: P.Type = B.self
let b = cast(metatype: pB, to: B.self)   //=&gt; an Optional&lt;Metatype&lt;B&gt;&gt;
</code></pre>

<h2 id="impactonexistingcode">Impact on existing code</h2>

<p>This is a source-breaking change that can be automated by a migrator. Any occurrence of <code>T.Type</code> or <code>T.Protocol</code> will be simply renamed to <code>Metatype&lt;T&gt;</code>.</p>

<h2 id="alternativesconsidered">Alternatives considered</h2>

<ul>
<li>Alternatively it’s reasonable to consider to rename <code>T.self</code> to <code>T.metatype</code>.</li>
<li>It was considered to reserve <code>Type&lt;T&gt;</code> for different usage in the future.</li>
</ul>

<p></p></div><div class="bloop_original_html"><style>body{font-family:Helvetica,Arial;font-size:13px}</style><div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;"><br></div><br><div id="bloop_sign_1469140728009710080" class="bloop_sign"><div style="font-family:helvetica,arial;font-size:13px">--&nbsp;<br>Adrian Zubarev<br>Sent with Airmail</div></div></div><div class="bloop_markdown"><p></p></div></body></html>