<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>Here is the new revised draft: <a href="https://gist.github.com/DevAndArtist/f7da7d7338eedb40adb9c5631a34aee1">https://gist.github.com/DevAndArtist/f7da7d7338eedb40adb9c5631a34aee1</a></p>
<p>We’d like to hear your feedback before we submit this revision. It’s unusual for such a quick revision, but it’s clearly was asked by Joe Groff and the feedback from the community.</p>
<hr>
<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="">[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>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<T>(of: T) -> 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<T></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<T>(to _: T.Type) -> Bool {
return Int.self is T.Type
}
intConforms(to: CustomStringConvertible.self) //=> false
Int.self is CustomStringConvertible.Type //=> 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<T>(to _: T.Type) -> Bool {
return Int.self is T
}
intConforms(to: CustomStringConvertible.Type.self) //=> 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<T>(to _: T.Type) -> Bool {
return Int.self is T
}
intConforms(to: Any.Type.self) //=> true
intConforms(to: Any.self) //=> true
Int.self is Any.Type //=> 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<T>(of _: T.Type) -> Bool {
return R.self is T
}
rIsSubtype(of: P.Type.self) //=> false
R.self is Any.Type //=> Always true
R.self is P.Type //=> true
R.self is R.Type //=> 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><T>(_: 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<T></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<T></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<T>(of instance: T) -> T.Type</code></li>
<li>after: <code>public func metatype<T>(of instance: T) -> Metatype<T></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`<T>(metatype: Metatype<Any>, also _: Metatype<T> ) -> Bool {
return metatype is Metatype<T>
}
`is`(metatype: R.self, also: Any.self) //=> true | Currently: false
`is`(metatype: R.self, also: P.self) //=> true | Currently: false
`is`(metatype: R.self, also: R.self) //=> true
`is`(metatype: B.self, also: Any.self) //=> true | Currently: false
`is`(metatype: B.self, also: P.self) //=> true | Currently: false
`is`(metatype: B.self, also: R.self) //=> true | Currently: false
`is`(metatype: B.self, also: A.self) //=> true
`is`(metatype: B.self, also: B.self) //=> true
func cast<T>(metatype: Metatype<Any>, to _: Metatype<T>) -> Metatype<T>? {
return metatype as? Metatype<T>
}
cast(metatype: R.self, to: Any.self) //=> an Optional<Metatype<Any>> | Currently: nil
cast(metatype: R.self, to: P.self) //=> an Optional<Metatype<P>> | Currently: nil
cast(metatype: R.self, to: R.self) //=> an Optional<Metatype<R>> | Currently: an Optional<R.Protocol>
let anyR: Any.Type = R.self
let r = cast(metatype: anyR, to: R.self) //=> an Optional<Metatype<R>> | Currently: an Optional<R.Protocol>
cast(metatype: B.self, to: Any.self) //=> an Optional<Metatype<Any>> | Currently: nil
cast(metatype: B.self, to: P.self) //=> an Optional<Metatype<P>> | Currently: nil
cast(metatype: B.self, to: R.self) //=> an Optional<Metatype<R>> | Currently: nil
cast(metatype: B.self, to: A.self) //=> an Optional<Metatype<A>>
cast(metatype: B.self, to: B.self) //=> an Optional<Metatype<B>>
let pB: P.Type = B.self
let b = cast(metatype: pB, to: B.self) //=> an Optional<Metatype<B>>
</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<T></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<T></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_1469133571250024960" 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 21. Juli 2016 um 22:22:38, 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 id="bloop_customfont" style="font-family:Helvetica,Arial;font-size:13px; color: rgba(0,0,0,1.0); margin: 0px; line-height: auto;">
Sliced revision is almost done. We would like to focus on two
things: `Metatype<T>` and `metype(of:)`. I’ll post it here
soon before submitting a PR. </div>
<br>
<div id="bloop_sign_1469132442946418944" class="bloop_sign">
<div style="font-family:helvetica,arial;font-size:13px">
-- <br>
Adrian Zubarev<br>
Sent with Airmail</div>
</div>
</div></div></span></blockquote></div><div class="bloop_markdown"><p></p></div></body></html>