<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>The problem with <code>Mirror</code> is that it can only carry a metatype around and thats it. You may be able to extract it from there and than what? You’ll cast it the old fashion way.</p>
<p>As I may recall I asked the community in the following thread about <code>Hashable</code> ability, which isn’t implementable without compiler magic and special internal protocols: <a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160627/023067.html">[Discussion] Can we make <code>.Type</code> Hashable?</a></p>
<p>Your last API for <code>Type<T></code> doesn’t solve any problems:</p>
<ol>
<li><code>var size: Int { return sizeof(T.self) }</code> is the wrong way to go.</li>
<li>If you extract a dynamic metatype from your <code>Mirror</code>, you still won’t be able to use <code>Type<T></code> to calculate the right <code>size</code> for it. Not possible with such design.</li>
<li>Static properties do the same as instance properties. With this we could just leave <code>MemoryLayout</code> and don’t need <code>Type<T></code> at all.</li>
</ol>
<p>You’re talking about the lack of the ability of dispatching static members (you also forget initializer), which is not right. We don’t remove the ability to do so. It just gets a bottleneck property <code>metatype</code>.</p>
<p>I’m busy completing your last gist with all the mentioned facts. Here is the last portion I just finished:</p>
<h3 id="metatypes">Metatypes</h3>
<p>Current metatypes <code>T.Type</code> have one additional feature that <code>Type<T></code> will not have (directly).
They can invoke static methods and initializer of types they reflect.</p>
<p>Thus, we cannot drop metatypes.
But they will be renamed: old <code>T.Type</code> will become <code>Metatype<T></code> and old <em>internal</em> <code>T.self</code> will become <code>T.metatype</code>, where the <em>public</em> <code>T.self</code> returns an instance of <code>Type<T></code>.</p>
<p>Example in Swift 2.2:</p>
<pre><code class="swift">protocol HasStatic { static func staticMethod() -> String }
struct A : HasStatic { static func staticMethod() -> String { return "A" } }
struct B : HasStatic { static func staticMethod() -> String { return "B" } }
func callStatic(_ metatype: HasStatic.Type) {
// `Type<T>` cannot do this, but there will be a
// bottleneck property `metatype` to achieve this.
let result = metatype.staticMethod()
print(result)
}
let a = A.self
let b = B.self
callStatic(a) //=> A
callStatic(b) //=> B
</code></pre>
<p>In other words, metatypes (continue to) allow dynamic polymorphism for static methods.</p>
<p>After this proposal we will have two distinct ways to achieve the same result:</p>
<pre><code class="swift">// Version 1:
func callStatic(_ metatype: Metatype<HasStatic>) {
let result = metatype.staticMethod()
print(result)
}
let a = A.self.metatype
let b = B.self.metatype
callStatic(a) //=> A
callStatic(b) //=> B
// Version 2:
func callStatic(_ type: Type<HasStatic>) {
let result = type.metatype.staticMethod()
print(result)
}
let a = Type<HasStatic>(casting: A.self)!
let b = Type<HasStatic>(casting: B.self)!
callStatic(a) //=> A
callStatic(b) //=> B
</code></pre>
<hr>
<p>Everything is still possible, only the casting becomes a little clumsy. Therefore I’ll add section about dynamic casts such as <code>as</code>, <code>as?</code>, <code>as!</code> and <code>is</code> which needs a little tweaking. As I already showed in my last post. It’s totally safe to introduce this tweaking post Swift 3.</p>
<p>Please stay patient and let me finish the addition. </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_1468673977684073984" 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 16. Juli 2016 um 14:19:08, Anton Zhilin (<a href="mailto:antonyzhilin@gmail.com">antonyzhilin@gmail.com</a>) schrieb:</p> <blockquote type="cite" class="clean_bq"><span><div><ol style="color: rgb(0, 0, 0); font-family: 'helvetica Neue', helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><li>My suggestion<span class="Apple-converted-space"> </span><code style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace; margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; display: inline; background-color: rgb(248, 248, 248);">
#2</code><ul style="margin: 0px; padding-left: 1em;"><li>Allow labelless initialization of<span class="Apple-converted-space"> </span><code style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace; margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; display: inline; background-color: rgb(248, 248, 248);">
Mirror</code><span class="Apple-converted-space"> </span>from metatype or from value</li><li>Move dynamic<span class="Apple-converted-space"> </span><code style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace; margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; display: inline; background-color: rgb(248, 248, 248);">
size</code>,<span class="Apple-converted-space"> </span><code style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace; margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; display: inline; background-color: rgb(248, 248, 248);">
stride</code>,<span class="Apple-converted-space"> </span><code style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace; margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; display: inline; background-color: rgb(248, 248, 248);">
alignment</code><span class="Apple-converted-space"> </span>to<span class="Apple-converted-space"> </span><code style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace; margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; display: inline; background-color: rgb(248, 248, 248);">
Mirror</code></li><li><code style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace; margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; display: inline; background-color: rgb(248, 248, 248);">
T.self</code><span class="Apple-converted-space"> </span>and future type literals will create instances of<span class="Apple-converted-space"> </span><code style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace; margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; display: inline; background-color: rgb(248, 248, 248);">
Type<T></code></li></ul></li></ol><pre style="color: rgb(0, 0, 0); font-size: 1em; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; font-family: Consolas, Inconsolata, Courier, monospace; line-height: 1.2em; margin: 1.2em 0px;"><code class="hljs language-swift" style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace; margin: 0px 0.15em; white-space: pre; overflow: auto; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; border: 1px solid rgb(204, 204, 204); padding: 0.5em; color: rgb(51, 51, 51); background-color: rgb(248, 248, 248); display: block !important; background-position: initial initial; background-repeat: initial initial;">struct Type<T> {
init()
var size: Int { return sizeof(T.self) }
var stride: Int { return strideof(T.self) }
var alignment: Int { return alignof(T.self) }
static var size: Int { return sizeof(T.self) }
static var stride: Int { return strideof(T.self) }
static var alignment: Int { return alignof(T.self) }
var metatype: Metatype<T> { return T.metatype }
static var metatype: Metatype<T> { return T.metatype }
// Nothing more; no fields!
}
</code></pre><ul style="color: rgb(0, 0, 0); font-family: 'helvetica Neue', helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><li>Metatypes will be typically created using<span class="Apple-converted-space"> </span><code style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace; margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; display: inline; background-color: rgb(248, 248, 248);">
Type<T>.metatype</code><span class="Apple-converted-space"> </span>and<span class="Apple-converted-space"> </span><code style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace; margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; display: inline; background-color: rgb(248, 248, 248);">
dynamicType</code></li><li><code style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace; margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; display: inline; background-color: rgb(248, 248, 248);">
Pro</code><span class="Apple-converted-space"> </span>Both static and dynamic<span class="Apple-converted-space"> </span><code style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace; margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; display: inline; background-color: rgb(248, 248, 248);">
size</code>,<span class="Apple-converted-space"> </span><code style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace; margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; display: inline; background-color: rgb(248, 248, 248);">
stride</code>,<span class="Apple-converted-space"> </span><code style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace; margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; display: inline; background-color: rgb(248, 248, 248);">
alignment</code><span class="Apple-converted-space"> </span>are in logically suited contexts</li><li><code style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace; margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; display: inline; background-color: rgb(248, 248, 248);">
Pro</code><span class="Apple-converted-space"> </span>Metatypes (with which few people will work) are separated from static types (which are used for function specialization etc)</li><li><code style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace; margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; display: inline; background-color: rgb(248, 248, 248);">
Con</code><span class="Apple-converted-space"> </span>Additional entity<span class="Apple-converted-space"> </span><code style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace; margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; display: inline; background-color: rgb(248, 248, 248);">
Type<T></code>, but I think it’s the most elegant of all solutions that introduce additional types</li></ul></div></span></blockquote></div><div class="bloop_markdown"><p></p></div></body></html>