<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>We’ve submitted a PR. <a href="https://github.com/apple/swift-evolution/pull/466">https://github.com/apple/swift-evolution/pull/466</a></p>
<p>The proposal was refactored completely. </p>
<hr>
<h2 id="introduction">Introduction</h2>
<p>This proposal removes <code>.Type</code> and <code>.Protocol</code> in favor of two generic-style syntaxes and aligns global <code>type(of:)</code> function (SE–0096) to match the changes.</p>
<p>Swift-evolution threads: </p>
<ul>
<li><a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160718/025115.html">[Revision] [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<T></code></a></li>
</ul>
<h2 id="motivation">Motivation</h2>
<p>Every type <code>T</code> has an instance, accessible through <code>T.self</code>, which represents the type itself. Like all instances in Swift, this “type instance” itself has a type, which is referred to as its “metatype”. The metatype of <code>T</code> is written <code>T.Type</code>. The instance members of the metatype are the same as the static or class members of the type.</p>
<p>Metatypes have subtype relationships which reflect the types they represent. For instance, given these types:</p>
<pre><code class="swift">protocol Proto {}
class Base {}
class Derived: Base, Proto {}
</code></pre>
<p><code>Derived.Type</code> is a subtype of both <code>Base.Type</code> and <code>Proto.Type</code> (and <code>Any.Type</code>). That means that <code>Derived.self</code> can be used anywhere a <code>Derived.Type</code>, <code>Base.Type</code>, <code>Proto.Type</code>, or <code>Any.Type</code> is called for.</p>
<p>Unfortunately, this simple picture is complicated by protocols. <code>Proto.self</code> is actually of type <code>Proto.Protocol</code>, not type <code>Proto.Type</code>. This is necessary because the protocol does not, and cannot, conform to itself; it requires conforming types to provide static members, but it doesn’t actually provide those members itself. <code>Proto.Type</code> still exists, but it is the supertype of all types conforming to the protocol.</p>
<p>Making this worse, a generic type always uses <code>T.Type</code> to refer to the type of <code>T.self</code>. So when <code>Proto</code> is bound to a generic parameter <code>P</code>, <code>P.Type</code> is the same as <code>Proto.Protocol</code>.</p>
<p>This shifting of types is complicated and confusing; we seek to clean up this area.</p>
<p>We also believe that, in the long term, the dot syntax will prevent us from implementing certain future enhancements that might be valuable:</p>
<ul>
<li>Moving the implementation of metatypes at least partly into the standard library.</li>
<li>Adding members available on all type instances for features like read-write reflection or memory layout information.</li>
<li>Conforming metatypes to protocols like <code>Hashable</code> or <code>CustomStringConvertible</code>.</li>
<li>Offering straightforward syntaxes for dynamic features like looking up types by name.</li>
</ul>
<h2 id="proposedsolution">Proposed solution</h2>
<p>We abolish <code>.Type</code> and <code>.Protocol</code> in favor of two generic-style syntaxes:</p>
<ul>
<li><p><code>Type<T></code> is the concrete type of <code>T.self</code>. A <code>Type<T></code> can only ever accept that one specific type, not any of its subtypes. If <code>T</code> is a protocol <code>P</code>, than the only supertype for <code>Type<P></code> is <code>Subtype<Any></code>. To be crystal clear here, <code>Type<P></code> is not a subtype of <code>Subtype<P></code>.</p></li>
<li><p><code>Subtype<T></code> is the supertype of all <code>Type</code>s whose instances are subtypes of <code>T</code>. If <code>T</code> is a class, <code>Subtype<T></code> would accept a <code>Type</code> for any of its subclasses. If <code>T</code> is a protocol, <code>Subtype<T></code> would accept a <code>Type</code> for any conforming concrete type.</p></li>
</ul>
<p>In this new notation, some of our existing standard library functions would have signatures like:</p>
<pre><code class="swift">func unsafeBitCast<T, U>(_: T, to type: Type<U>) -> U
func sizeof<T>(_: Type<T>) -> Int
func ==(t0: Subtype<Any>?, t1: Subtype<Any>?) -> Bool
func type<T>(of: T) -> Subtype<T> // SE-0096
</code></pre>
<p>That last example, <code>type(of:)</code>, is rather interesting, because it is actually a magic syntax rather than a function. We propose to align this syntax with <code>Type</code> and <code>Subtype</code> by renaming it to <code>Subtype(of:)</code>. We believe this is clearer about both the type and meaning of the operation.</p>
<pre><code class="swift">let instance: NSObject = NSString()
let class: Subtype<NSObject> = Subtype(of: instance)
print(class) // => NSString
</code></pre>
<p></p><details><summary><strong>Example: visual metatype relationship</strong></summary><p></p>
<p>Types:</p>
<pre><code class="swift">protocol P { static func foo() }
protocol R : P { static func boo() }
class A : P { static func foo() { ... } }
class B : A, R { static func boo() { ... } }
</code></pre>
<p><code>Subtype</code> relationship (not a valid Swift code):</p>
<pre><code class="swift">Subtype<Any> {
var self: Self { get }
}
Subtype<P> : Subtype<Any> {
func foo()
}
Subtype<R> : Subtype<P> {
func boo()
}
Subtype<A> : Subtype<P> { }
Subtype<B> : Subtype<A>, Subtype<R> { }
</code></pre>
<p><code>Type</code> relationship (not a valid Swift code):</p>
<pre><code class="swift">// `Type` of a protocol is blind
Type<P> : Subtype<Any> { }
// `Type` of a protocol is blind
Type<R> : Subtype<Any> { }
Type<A> : Subtype<A> { }
Type<B> : Subtype<B> { }
</code></pre>
<p>Example:</p>
<pre><code class="swift">let a1: Type<A> = A.self // Okay
let p1: Type<P> = P.self // Okay
let p2: Type<P> = C.self // Error -- `C` is not the same as `P`
let any_1: Subtype<Any> = A.self // Okay
let any_2: Subtype<Any> = P.self // Okay
let a_1: Subtype<A> = A.self // Okay
let p_1: Subtype<P> = A.self // Okay
let p_2: Subtype<P> = P.self // Error -- `Type<P>` is not a subtype of `Subtype<P>`
</code></pre>
<p></p></details><p></p>
<p></p><details><summary><strong>Example: generic functions</strong></summary><p></p>
<pre><code class="swift">func dynamic<T>(type: Subtype<Any>, is _: Type<T>) -> Bool {
return type is Subtype<T>
}
func dynamic<T>(type: Subtype<Any>, as _: Type<T>) -> Subtype<T>? {
return type as? Subtype<T>
}
protocol Proto {}
struct Struct: Proto {}
let s1: Type<Struct> = Struct.self
dynamic(type: s1, is: Proto.self) //=> true
dynamic(type: s1, as: Proto.self) //=> an `Optional<Subtype<Proto>>`
</code></pre>
<p></p></details><p></p>
<h2 id="futuredirections">Future Directions</h2>
<ul>
<li><p>We could allow extensions on <code>Type</code> and perhaps on <code>Subtype</code> to add members or conform them to protocols. This could allow us to remove some standard library hacks, like the non-<code>Equatable</code>-related <code>==</code> operators for types.</p></li>
<li><p>It may be possible to implement parts of <code>Type</code> as a fairly ordinary final class, moving code from the runtime into the standard library.</p></li>
<li><p>We could offer a <code>Subtype(ofType: Type<T>, named: String)</code> pseudo-initializer which would allow type-safe access to classes by name.</p></li>
<li><p>We could offer other reflection and dynamic features on <code>Type</code> and <code>Subtype</code>.</p></li>
<li><p>We could move the <code>MemoryLayout</code> members into <code>Type</code> (presumably prefixed), removing the rather artificial <code>MemoryLayout</code> enum.</p></li>
<li><p>Along with other generics enhancements, there may be a use for a <code>Subprotocol<T></code> syntax for any protocol requiring conformance to protocol <code>T</code>.</p></li>
</ul>
<h2 id="impactonexistingcode">Impact on existing code</h2>
<p>This is a source-breaking change that can be automated by a migrator. </p>
<p>We suggest the following migration process; this can differ from the final migration process implemented by the core team if this proposal will be accepted:</p>
<ul>
<li><code>Any.Type</code> is migrated to <code>Subtype<Any></code>.</li>
<li>If <code>T.Type</code> is in function parameter, where <code>T</code> is a generic type parameter, then it’s migrated to <code>Type<T></code>.</li>
<li>Every <code>T.Protocol</code> will be replaced with <code>Type<T></code>.</li>
<li>Every <code>T.Type</code> in a dynamic cast will be replaced with <code>Subtype<T></code>.</li>
<li>If static members are called on a metatype instance, then this instance is migrated to <code>Subtype<T></code>.</li>
<li>Return types of functions are migrated to <code>Subtype<T></code>.</li>
<li>Variable declarations is migrated to <code>Subtype<T></code>.</li>
</ul>
<h2 id="alternativesconsidered">Alternatives considered</h2>
<p>Other names for <code>Type</code> and <code>Subtype</code> were considered:</p>
<ul>
<li>Type: SpecificType, Metatype or ExactType.</li>
<li>Subtype: Supertype, Base, BaseType, ExistentialType or TypeProtocol.</li>
</ul>
<p>Alternatively the pseudo initializer <code>Subtype(of:)</code> could remain as a global function:</p>
<pre><code class="swift">public func subtype<T>(of instance: T) -> Subtype<T>
</code></pre>
<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_1469475221643683840" class="bloop_sign"><div style="font-family:helvetica,arial;font-size:13px">-- <br>Adrian Zubarev<br>Sent with Airmail</div></div></div><div class="bloop_markdown"><p></p></div></body></html>