<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>PR closed, we’ll come back for Swift 3.1. ;)</p>
<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_1469481795120284928" class="bloop_sign"><div style="font-family:helvetica,arial;font-size:13px">-- <br>Adrian Zubarev<br>Sent with Airmail</div></div> <br><p class="airmail_on">Am 25. Juli 2016 um 23:08:26, Adrian Zubarev (<a href="mailto:adrian.zubarev@devandartist.com">adrian.zubarev@devandartist.com</a>) schrieb:</p> <blockquote type="cite" class="clean_bq"><span><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div></div><div>
<title></title>
<div class="bloop_markdown">
<p>Fixed PR: <a href="https://github.com/apple/swift-evolution/pull/468">https://github.com/apple/swift-evolution/pull/468</a></p>
</div>
<div class="bloop_original_html">
<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_1469480883144754944" class="bloop_sign">
<div style="font-family:helvetica,arial;font-size:13px">
-- <br>
Adrian Zubarev<br>
Sent with Airmail</div>
</div>
<br>
<p class="airmail_on">Am 25. Juli 2016 um 21:35:22, Adrian Zubarev
(<a href="mailto:adrian.zubarev@devandartist.com">adrian.zubarev@devandartist.com</a>)
schrieb:</p>
<blockquote type="cite" class="clean_bq">
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">
<div>
<div class="bloop_markdown">
<p><span>We’ve submitted a PR. <a href="https://github.com/apple/swift-evolution/pull/466">https://github.com/apple/swift-evolution/pull/466</a></span></p>
<p><span>The proposal was refactored completely.</span></p>
<hr>
<h2 id="introduction"><span>Introduction</span></h2>
<p><span>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.</span></p>
<p><span>Swift-evolution threads:</span></p>
<ul>
<li><span><a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160718/025115.html">
[Revision] [Pitch] Rename <code>T.Type</code></a></span></li>
<li><span><a href="">[Review] SE–0126: Refactor Metatypes,
repurpose T[dot]self and Mirror</a></span></li>
<li><span><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></span></li>
<li><span><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></span></li>
</ul>
<h2 id="motivation"><span>Motivation</span></h2>
<p><span>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.</span></p>
<p><span>Metatypes have subtype relationships which reflect the
types they represent. For instance, given these types:</span></p>
<pre><span><code class="swift">protocol Proto {}
class Base {}
class Derived: Base, Proto {}
</code></span></pre>
<p><span><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.</span></p>
<p><span>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.</span></p>
<p><span>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>.</span></p>
<p><span>This shifting of types is complicated and confusing; we
seek to clean up this area.</span></p>
<p><span>We also believe that, in the long term, the dot syntax
will prevent us from implementing certain future enhancements that
might be valuable:</span></p>
<ul>
<li><span>Moving the implementation of metatypes at least partly
into the standard library.</span></li>
<li><span>Adding members available on all type instances for
features like read-write reflection or memory layout
information.</span></li>
<li><span>Conforming metatypes to protocols like
<code>Hashable</code> or
<code>CustomStringConvertible</code>.</span></li>
<li><span>Offering straightforward syntaxes for dynamic features
like looking up types by name.</span></li>
</ul>
<h2 id="proposedsolution"><span>Proposed solution</span></h2>
<p><span>We abolish <code>.Type</code> and <code>.Protocol</code>
in favor of two generic-style syntaxes:</span></p>
<ul>
<li>
<p><span><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>.</span></p>
</li>
<li>
<p><span><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.</span></p>
</li>
</ul>
<p><span>In this new notation, some of our existing standard
library functions would have signatures like:</span></p>
<pre><span><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></span></pre>
<p><span>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.</span></p>
<pre><span><code class="swift">let instance: NSObject = NSString()
let class: Subtype<NSObject> = Subtype(of: instance)
print(class) // => NSString
</code></span></pre>
<details>
<summary><span><strong>Example: visual metatype
relationship</strong></span></summary>
<p><span>Types:</span></p>
<pre><span><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></span></pre>
<p><span><code>Subtype</code> relationship (not a valid Swift
code):</span></p>
<pre><span><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></span></pre>
<p><span><code>Type</code> relationship (not a valid Swift
code):</span></p>
<pre><span><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></span></pre>
<p><span>Example:</span></p>
<pre><span><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></span></pre></details>
<details>
<summary><span><strong>Example: generic
functions</strong></span></summary>
<pre><span><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></span></pre></details>
<h2 id="futuredirections"><span>Future Directions</span></h2>
<ul>
<li>
<p><span>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.</span></p>
</li>
<li>
<p><span>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.</span></p>
</li>
<li>
<p><span>We could offer a <code>Subtype(ofType: Type<T>,
named: String)</code> pseudo-initializer which would allow
type-safe access to classes by name.</span></p>
</li>
<li>
<p><span>We could offer other reflection and dynamic features on
<code>Type</code> and <code>Subtype</code>.</span></p>
</li>
<li>
<p><span>We could move the <code>MemoryLayout</code> members into
<code>Type</code> (presumably prefixed), removing the rather
artificial <code>MemoryLayout</code> enum.</span></p>
</li>
<li>
<p><span>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>.</span></p>
</li>
</ul>
<h2 id="impactonexistingcode"><span>Impact on existing
code</span></h2>
<p><span>This is a source-breaking change that can be automated by
a migrator.</span></p>
<p><span>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:</span></p>
<ul>
<li><span><code>Any.Type</code> is migrated to
<code>Subtype<Any></code>.</span></li>
<li><span>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>.</span></li>
<li><span>Every <code>T.Protocol</code> will be replaced with
<code>Type<T></code>.</span></li>
<li><span>Every <code>T.Type</code> in a dynamic cast will be
replaced with <code>Subtype<T></code>.</span></li>
<li><span>If static members are called on a metatype instance, then
this instance is migrated to
<code>Subtype<T></code>.</span></li>
<li><span>Return types of functions are migrated to
<code>Subtype<T></code>.</span></li>
<li><span>Variable declarations is migrated to
<code>Subtype<T></code>.</span></li>
</ul>
<h2 id="alternativesconsidered"><span>Alternatives
considered</span></h2>
<p><span>Other names for <code>Type</code> and <code>Subtype</code>
were considered:</span></p>
<ul>
<li><span>Type: SpecificType, Metatype or ExactType.</span></li>
<li><span>Subtype: Supertype, Base, BaseType, ExistentialType or
TypeProtocol.</span></li>
</ul>
<p><span>Alternatively the pseudo initializer
<code>Subtype(of:)</code> could remain as a global
function:</span></p>
<pre><span><code class="swift">public func subtype<T>(of instance: T) -> Subtype<T>
</code></span></pre></div>
<div class="bloop_original_html">
<div id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">
<span><br></span></div>
<span><br></span>
<div id="bloop_sign_1469475221643683840" class="bloop_sign">
<div style="font-family:helvetica,arial;font-size:13px">
<span>-- <br>
Adrian Zubarev<br>
Sent with Airmail</span></div>
</div>
</div>
<div class="bloop_markdown"></div>
</div>
</div>
</blockquote>
</div>
<div class="bloop_markdown"></div>
</div></div></span></blockquote></div><div class="bloop_markdown"><p></p></div></body></html>