<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>Formatted version: <a href="https://github.com/DevAndArtist/swift-evolution/blob/refactor_metatypes/proposals/0126-refactor-metatypes.md">https://github.com/DevAndArtist/swift-evolution/blob/refactor_metatypes/proposals/0126-refactor-metatypes.md</a></p>
<hr>
<h1 id="refactormetatypes">Refactor Metatypes</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>, <a href="https://github.com/brentdax">Brent Royal-Gordon</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 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 thread (post Swift 3): </p>
<ul>
<li><a href="">[Pitch] Refactor Metatypes</a></li>
</ul>
<p>Older swift-evolution threads: <a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160718/025115.html">[1]</a>, <a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160718/024772.html">[2]</a>, <a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160704/023818.html">[3]</a></p>
<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> only ever has one instance, <code>T.self</code>; even if <code>T</code> has a subtype <code>U</code>, <code>Type<U></code> is not a subtype of <code>Type<T></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>, including <code>T</code> itself:</p></li>
<li><p>If <code>T</code> is a struct or enum, then <code>Type<T></code> is the only subtype of <code>Subtype<T></code>.</p></li>
<li><p>If <code>T</code> is a class, then <code>Type<T></code> and the <code>Type</code>s of all subclasses of <code>T</code> are subtypes of <code>Subtype<T></code>.</p></li>
<li><p>If <code>T</code> is a protocol, then the <code>Type</code>s of all concrete types conforming to <code>T</code> are subtypes of <code>Subtype<T></code>. <code>Type<T></code> is not itself a subtype of <code>Subtype<T></code>, or of any <code>Subtype</code> other than <code>Subtype<Any></code>.</p></li>
<li><p>Structural types follow the subtype/supertype relationships of their constituent types. For instance:</p></li>
<li><p><code>Type<(NSString, NSString)></code> is a subtype of <code>Subtype<(NSObject, NSObject)></code></p></li>
<li><p>Metatypes of functions are a little bit more special (<a href="https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science)">the subtyping relation on functions flips around for parameter types</a>):</p>
<ul>
<li><code>Type<(Any) -> Void></code> is a subtype of <code>Subtype<(Int) -> Void></code> etc.</li>
<li><code>Type<(Void) -> Int></code> is a subtype of <code>Subtype<(Void) -> Any></code></li>
</ul></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 ==(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 anInstance: NSObject = NSString()
let aClass: Subtype<NSObject> = Subtype(of: anInstance)
print(aClass) // => NSString
</code></pre>
<h4 id="moredetails:">More details:</h4>
<ul>
<li><p>Every static or class member of <code>T</code> which can be called on all subtypes is an instance member of <code>Subtype<T></code>. That includes:</p></li>
<li><p>Static/class properties and methods</p></li>
<li><p>Required initializers (as methods named <code>init</code>)</p></li>
<li><p>Unbound instance methods</p></li>
<li><p>The <code>Type<T></code> of a concrete type <code>T</code> has all of the members required by <code>Subtype<T></code>, plus non-required initializers.</p></li>
<li><p>The <code>Type<T></code> of a protocol <code>T</code> includes only unbound instance methods of <code>T</code>.</p></li>
<li><p>If <code>T</code> conforms to <code>P</code>, then <code>Subtype<T></code> is a subtype of <code>Subtype<P></code>, even if <code>T</code> is a protocol.</p></li>
<li><p>The type of <code>Subtype<T>.self</code> is <code>Type<Subtype<T>></code>.</p></li>
<li><p>The type of <code>Type<T>.self</code> is <code>Type<Type<T>></code>, which is not a subtype of any type except <code>Subtype<Type<T>></code>. There is an infinite regress of <code>Type<...<Type<T>>></code>s.</p></li>
<li><p><code>Subtype</code>s are abstract types similar to class-bound protocols; they, too, support identity operations. </p></li>
<li><p><code>Type</code>s are concrete reference types which have identities just like objects do.</p></li>
</ul>
<p><code>swift
Int.self === Int.self // true
Int.self === Any.self // false
</code></p>
<p></p><details><summary><strong>Visual metatype relationship example (not a valid Swift code)</strong></summary><p></p>
<pre><code class="swift">protocol Foo {
static func foo()
func instanceMethodFoo()
}
protocol Boo : Foo {
static func foo()
static func boo()
func instanceMethodFoo()
func instanceMethodBoo()
}
class A : Foo {
static func foo() { ... }
func instanceMethodFoo() { ... }
}
class B : A, Boo {
static func boo() { ... }
func instanceMethodBoo() { ... }
}
/// Swift generates metatypes along the lines of:
///
/// Syntax: `meta protocol Subtype<T>` - only metatypes can conform to these meta protocols
/// Syntax: `final meta class Type<T>` - metatype
/// Note: `CapturedType` represents `Self` of `T` in `Subtype<T>`
// For Any:
meta protocol Subtype<Any> : meta class {
var `self`: Self { get }
}
final meta class Type<Any> : Subtype<Any> {
var `self`: Type<Any> { ... }
}
// For Foo:
meta protocol Subtype<Foo> : Subtype<Any> {
var `self`: Self { get }
func foo()
func instanceMethodFoo(_ `self`: CapturedType) -> (Void) -> Void
}
final meta class Type<Foo> : Subtype<Any> {
var `self`: Type<Foo> { ... }
func instanceMethodFoo(_ `self`: Foo) -> (Void) -> Void { ... }
}
// For Boo:
meta protocol Subtype<Boo> : Subtype<Foo> {
var `self`: Self { get }
func boo()
func instanceMethodBoo(_ `self`: CapturedType) -> (Void) -> Void
}
final meta class Type<Boo> : Subtype<Any> {
var `self`: Type<Boo> { ... }
func instanceMethodFoo(_ `self`: Boo) -> (Void) -> Void { ... }
func instanceMethodBoo(_ `self`: Boo) -> (Void) -> Void { ... }
}
// For A:
meta protocol Subtype<A> : Subtype<Foo> {
var `self`: Self { get }
func foo()
func instanceMethodFoo(_ `self`: CapturedType) -> (Void) -> Void
}
final meta class Type<A> : Subtype<A> {
var `self`: Type<A> { ... }
func foo() { ... }
func instanceMethodFoo(_ `self`: A) -> (Void) -> Void { ... }
}
// For B:
meta protocol Subtype<B> : Subtype<A>, Subtype<Boo> {
var `self`: Self
func foo()
func boo()
func instanceMethodFoo(_ `self`: CapturedType) -> (Void) -> Void
func instanceMethodBoo(_ `self`: CapturedType) -> (Void) -> Void
}
final meta class Type<B> : Subtype<B> {
var `self`: Type<B> { ... }
func foo() { ... }
func boo() { ... }
func instanceMethodFoo(_ `self`: B) -> (Void) -> Void { ... }
func instanceMethodBoo(_ `self`: B) -> (Void) -> Void { ... }
}
</code></pre>
<p></p></details><p></p>
<p></p><details><summary><strong>Some examples</strong></summary><p></p>
<pre><code class="swift">// Types:
protocol Foo {}
protocol Boo : Foo {}
class A : Foo {}
class B : A, Boo {}
struct S: Foo {}
// Metatypes:
let a1: Type<A> = A.self //=> Okay
let p1: Type<Foo> = Foo.self //=> Okay
let p2: Type<Boo> = C.self //=> Error -- `C` is not the same as `Foo`
let any_1: Subtype<Any> = A.self //=> Okay
let any_2: Subtype<Any> = Foo.self //=> Okay
let a_1: Subtype<A> = A.self //=> Okay
let p_1: Subtype<Foo> = A.self //=> Okay
let p_2: Subtype<Foo> = Foo.self //=> Error -- `Type<Foo>` is not a subtype of `Subtype<Foo>`
// Generic functions:
func dynamic<T>(subtype: Subtype<Any>, `is` _: Type<T>) -> Bool {
return type is Subtype<T>
}
func dynamic<T>(subtype: Subtype<Any>, `as` _: Type<T>) -> Subtype<T>? {
return type as? Subtype<T>
}
let s1: Type<S> = S.self
dynamic(subtype: s1, is: Foo.self) //=> true
dynamic(subtype: s1, as: Foo.self) //=> an `Optional<Subtype<Foo>>`
</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_1475057787908112896" 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>