<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><p>This is the latest draft of the proposal: <a href="https://github.com/DevAndArtist/swift-evolution/blob/refactor_existential_metatypes/proposals/0126-refactor-metatypes.md">https://github.com/DevAndArtist/swift-evolution/blob/refactor_existential_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="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160926/027341.html">[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>AnyType<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>AnyType<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>AnyType<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>AnyType<T></code>. <code>Type<T></code> is not itself a subtype of <code>AnyType<T></code>, or of any <code>AnyType</code> other than <code>AnyType<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>AnyType<(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>AnyType<(Int) -> Void></code> etc.</li>
<li><code>Type<(Void) -> Int></code> is a subtype of <code>AnyType<(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: AnyType<Any>?, t1: AnyType<Any>?) -> Bool
func type<T>(of instace: T) -> AnyType<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>AnyType</code> by correcting the return type to <code>AnyType<T></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: AnyType<NSObject> = type(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>AnyType<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>AnyType<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>AnyType<T></code> is a subtype of <code>AnyType<P></code>, even if <code>T</code> is a protocol.</p></li>
<li><p>The type of <code>AnyType<T>.self</code> is <code>Type<AnyType<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>AnyType<Type<T>></code>. There is an infinite regress of <code>Type<...<Type<T>>></code>s.</p></li>
<li><p><code>AnyType</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 AnyType<T>` - only metatypes can conform to these meta protocols
/// Syntax: `final meta class Type<T>` - metatype
/// Note: `CapturedType` represents `Self` of `T` in `AnyType<T>`
// For Any:
meta protocol AnyType<Any> : meta class {
var `self`: Self { get }
}
final meta class Type<Any> : AnyType<Any> {
var `self`: Type<Any> { ... }
}
// For Foo:
meta protocol AnyType<Foo> : AnyType<Any> {
var `self`: Self { get }
func foo()
func instanceMethodFoo(_ `self`: CapturedType) -> (Void) -> Void
}
final meta class Type<Foo> : AnyType<Any> {
var `self`: Type<Foo> { ... }
func instanceMethodFoo(_ `self`: Foo) -> (Void) -> Void { ... }
}
// For Boo:
meta protocol AnyType<Boo> : AnyType<Foo> {
var `self`: Self { get }
func boo()
func instanceMethodBoo(_ `self`: CapturedType) -> (Void) -> Void
}
final meta class Type<Boo> : AnyType<Any> {
var `self`: Type<Boo> { ... }
func instanceMethodFoo(_ `self`: Boo) -> (Void) -> Void { ... }
func instanceMethodBoo(_ `self`: Boo) -> (Void) -> Void { ... }
}
// For A:
meta protocol AnyType<A> : AnyType<Foo> {
var `self`: Self { get }
func foo()
func instanceMethodFoo(_ `self`: CapturedType) -> (Void) -> Void
}
final meta class Type<A> : AnyType<A> {
var `self`: Type<A> { ... }
func foo() { ... }
func instanceMethodFoo(_ `self`: A) -> (Void) -> Void { ... }
}
// For B:
meta protocol AnyType<B> : AnyType<A>, AnyType<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> : AnyType<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: AnyType<Any> = A.self //=> Okay
let any_2: AnyType<Any> = Foo.self //=> Okay
let a_1: AnyType<A> = A.self //=> Okay
let p_1: AnyType<Foo> = A.self //=> Okay
let p_2: AnyType<Foo> = Foo.self //=> Error -- `Type<Foo>` is not a subtype of `AnyType<Foo>`
// Generic functions:
func dynamic<T>(type: AnyType<Any>, `is` _: Type<T>) -> Bool {
return type is AnyType<T>
}
func dynamic<T>(type: AnyType<Any>, `as` _: Type<T>) -> AnyType<T>? {
return type as? AnyType<T>
}
let s1: Type<S> = S.self
dynamic(type: s1, is: Foo.self) //=> true
dynamic(type: s1, as: Foo.self) //=> an `Optional<AnyType<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>AnyType</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 new global function which would allow type-safe access to classes by name.</p>
<pre><code class="swift">func subtype<T : AnyObject>(of type: Type<T>, named: String) -> AnyType<T>? { ... }
</code></pre></li>
<li><p>We could offer other reflection and dynamic features on <code>Type</code> and <code>AnyType</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>
</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>AnyType<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>AnyType<T></code>.</li>
<li>If static members are called on a metatype instance, then this instance is migrated to <code>AnyType<T></code>.</li>
<li>Return types of functions are migrated to <code>AnyType<T></code>.</li>
<li>Variable declarations is migrated to <code>AnyType<T></code>.</li>
</ul>
<h2 id="alternativesconsidered">Alternatives considered</h2>
<p>Other names for <code>Type</code> and <code>AnyType</code> were considered:</p>
<ul>
<li>Type: SpecificType, Metatype or ExactType.</li>
<li>AnyType: Subtype, Supertype, Base, BaseType, ExistentialType, ExistentialMetatype or TypeProtocol.</li>
</ul></body></html>