<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>Good morning everyone. Welcome to our discussion Brent.</p>
<p>There is nothing bad about <code>Type<T></code> being a <code>final class</code>. To be honest I played with the idea of <code>Type<T></code> being a class for optimization purposes, because it does feel like a performance drop if we had to instantiate tons of the same types (which may or may not be really heavy):</p>
<pre><code class="swift">func dynamicType<T>(_ instance: T) -> Type<T>
let someHeavyViewController = …
dynamicType(someHeavyViewController)
dynamicType(someHeavyViewController)
dynamicType(someHeavyViewController)
dynamicType(someHeavyViewController)
dynamicType(someHeavyViewController)
</code></pre>
<p>With structs we would do the heavy work over and over again which can decrease our performance. </p>
<p>The reason why I dropped the class was because I though someone might subclass it, and I totally forgot about <code>final</code>.</p>
<p>If we had <code>public final class Type<T></code> we can optimize the above example and return a reference to the specific <code>Type<T></code> we already instantiated once.</p>
<p>Bikeshedding:</p>
<pre><code class="swift">internal var _typeStore: [Int: Type<Any>] = [:]
extension Type {
internal static var sharedInstance: Type<T> {
let identifier = _uniqueIdentifierOf(Type<T>.metatype)
if let typeReference = Type<T>(casting: _typeStore[identifier]) {
return typeReference
}
let newType = Type<T>()
// downcast `T` to `Any` but still store `T` in `_underlyingMetatype`
if let downcastedType = Type<Any>(casting: newType) {
_typeStore[identifier] = downcastedType
}
return newType
}
}
</code></pre>
<p>Now we can use <code>Type<T>.sharedInstance</code> to instantiate it or just to return an existing instance. </p>
<p>I did some tweaking to the <code>Type<T></code> api to make this work. I also filed a bug <a href="https://bugs.swift.org/browse/SR-2085">SR–2085</a> where <code>Metatype<T></code> does not behave like <code>T.Type</code> which is strange. Furthermore the api uses a workaround <code>T.Metatype</code> (renamed <code>T.Type</code>). </p>
<p><code>init?(casting: Type<T>?)</code> is now optional, to make <code>sharedInstance</code> work with the dictionary. This change doesn’t hurt at all.</p>
<p>I also implemented a check if <code>T</code> is <code>Any</code> to make downcasting to <code>Any</code> work as expected (<code>T.self is Any.Type</code> is the wrong way to go, because it’s always true no matter what). Took me a few hours to debug and fix these strange bugs.</p>
<p>You can check the (not compiling) gist here: <a href="https://gist.github.com/DevAndArtist/a5744f21107812b2d4e6baee2c55e0bf">https://gist.github.com/DevAndArtist/a5744f21107812b2d4e6baee2c55e0bf</a>.</p>
<p>If anyone want me to implement a version that does compile, just let me know. It’s already possible, but I’ll have to use <code>.self</code>, <code>.Type</code> instead of <code>.metatype</code> and <code>Metatype<T></code>.</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_1468564572096862976" 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 15. Juli 2016 um 08:25:40, Brent Royal-Gordon (<a href="mailto:brent@architechies.com">brent@architechies.com</a>) schrieb:</p> <blockquote type="cite" class="clean_bq"><span><div><div></div><div>> On Jul 14, 2016, at 6:33 PM, Anton Zhilin <antonyzhilin@gmail.com> wrote:
<br>>
<br>> And such a feature would look odd on a struct.
<br>
<br>But why would it be a struct? Types are obvious candidates to have identities; the type is the same "thing" everywhere it's referred to. Types don't have value semantics; if you perform a mutating operation on a type, that mutation is visible everywhere. Types (at least class types) have subtype relationships; structs can't express those.
<br>
<br>I like the `Type<>` syntax, especially if it's something that could at least partially be written in the standard library (how nice would it be if we had `final class Type<Describing>` in the generated headers?), and I really like the idea of extending a metatype to conform to a protocol. But a lot of what's being discussed here, I just don't get. What's the benefit of switching to structs? Of removing type members?
<br>
<br>> I don't see any problems with Type<Type<T>>. There is finite number of types that are used in the program, and compiler can collect and store information about them all statically. Am I right?
<br>
<br>Maybe not:
<br>
<br>        func recursivelyPrint<T>(type: T.Type, depth: Int) {
<br>                print(type)
<br>                guard depth > 0 else { return }
<br>                recursivelyPrint(type: type.dynamicType, depth: depth - 1)
<br>        }
<br>        recursivelyPrint(type: Int.self, depth: 5)
<br>
<br>--
<br>Brent Royal-Gordon
<br>Architechies
<br>
<br></div></div></span></blockquote></div><div class="bloop_markdown"><p></p></div></body></html>