<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>I’m still not fully convinced about fully removing the access to the metatype.</p>
<p>If you look at Ericas and Daves current proposal for <code>MemoryLayout</code> it would completely break and it would become impossible to build something like this.</p>
<p>And yes I borrowed the idea of putting properties like <code>size</code> into <code>Type<T></code> from SE–0101. That was mentioned in my previews posts.</p>
<p>If we remove <code>.Type</code>, how’d we access static members in other types like <code>MemoryLayout<T></code>?</p>
<ul>
<li>This is another contra argument to consider.</li>
</ul>
<p><strong>@Erica @Dave</strong>: Any statement on our conversation about <code>Type<T></code>?</p>
<p>I apologize that this whole idea raised in parallel to your proposal.</p>
<p>PS: We can merge our ideas into a single formal proposal. Ping me off-list when you get some time tomorrow.</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_1468443659827557120" 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 13. Juli 2016 um 22:57:40, Anton Zhilin (<a href="mailto:antonyzhilin@gmail.com">antonyzhilin@gmail.com</a>) schrieb:</p> <blockquote type="cite" class="clean_bq"><span><div><div></div><div>
<title></title>
<div dir="ltr">
<div>An even better explanation of my suggestion: instead
of <b>sealing</b> <font face="monospace, monospace">T.Type</font>, I suggest to <b>rename</b> it
to <font face="monospace, monospace">Type<T></font>, formally
turning it into a struct, and then add size and align properties to
it.</div>
<div>And regardless of how you interpret it, I suggest to remove
<font face="monospace, monospace">.Type</font>
notation.<br></div>
<div><br></div>
<div><span style="font-size:12.8px">> In Swift, only class
instances and metatypes have unique identities. There is no notion
of identity for structs, enums, functions, or tuples.</span></div>
<div><span style="font-size:12.8px">So <font face="monospace, monospace">Type<T></font> will lose identity.
That's fine. They will contain unique integer identifiers (assigned
to types during compilation) to do its work.</span></div>
<div><br></div>
<div><span style="font-size:12.8px">> </span><span style="font-size:12.8px">There is no special compiler magic
needed</span></div>
<div><span style="font-size:12.8px">Maybe. At least, this behaviour
will be needed in construction from type literals:</span></div>
<div><br></div>
<div><font face="monospace, monospace">func foo(_ x:
Type<BaseClass>)</font></div>
<div><font face="monospace, monospace">foo(DerivedClass) //
ok</font></div>
<div><font face="monospace, monospace">foo(Int)
// compilation error</font></div>
<div><br></div>
<div><font face="arial, helvetica, sans-serif">With </font><font face="monospace, monospace">Type<T></font>, I can see reflection
coming! For example, <font face="monospace, monospace">Type<T></font> can have the
following property (although we shouldn't right now):</div>
<div><br></div>
<div><font face="monospace, monospace">var fields: [String:
(Type<Any>, (T) -> Any)] { get }</font></div>
<div><br></div>
<div>I suggest you to prepare a formal proposal, or I will try
tomorrow. I would take the direction on removal of <font face="monospace, monospace">T.Type</font> notation.</div>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">2016-07-13 22:48 GMT+03:00 Adrian Zubarev
via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span>:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div style="word-wrap:break-word">
<div>
<p>Okay I get it now. You meant the size for a metatype
<code>sizeof(T.Type.self)</code>. This is indeed 8 bytes, at least
not for optimized <code>Bool</code> (as you showed).</p>
<blockquote>
<p>Internally, Type contains an Int, i.e. identifier of a type. For
every type, compiler must choose a different identifier</p>
</blockquote>
<p>We can already implement this with <code>Hashable</code>
protocol.</p>
<blockquote>
<p>ObjectIdentifier: A unique identifier for a class instance or
metatype.</p>
<p>In Swift, only class instances and metatypes have unique
identities. There is no notion of identity for structs, enums,
functions, or tuples.</p>
</blockquote>
<pre><code>// version 1:
public let hashValue: Int = ObjectIdentifier(T.self).hashValue
// version 2 (uses ObjectIdentifier calculation without
// constructing an instance of ObjectIdentifier):
init() {
// calculate the hashValue only once
// I'd rename `.self` to `.metatype`
let rawPointerMetatype = unsafeBitCast(T.self, to: Builtin.RawPointer.self)
self.hashValue = Int(Builtin.ptrtoint_Word(rawPointerMetatype))
}
public let hashValue: Int
</code></pre>
<blockquote>
<p>API of Type is defined so that it can only contain identifiers
of subtypes of T</p>
<p>For example, when you get a variable of Type, it can correspond
to BaseClass or DerivedClass</p>
</blockquote>
<p>I did a quick test and I feel like this falls under the part of
tweaking dynamic casts to work with <code>Type<T></code>.
There is no special compiler magic needed,
<code>unsafeBitCast</code> should do the trick. Or we should teach
dynamic casts to work with the inner type <code>T</code> instead of
the whole <code>Type<T></code>.</p>
<pre><code>public struct Type<T> : Hashable {
public let metatype: T.Type = T.self
public let hashValue: Int = ObjectIdentifier(T.self).hashValue
}
public func ==<T, U>(lhs: Type<T>, rhs: Type<U>) -> Bool {
return lhs.hashValue == rhs.hashValue
}
public func asOptionalCast<U, T>(type: Type<T>) -> Type<U>? {
guard (type.metatype as? U.Type) != nil else {
return nil
}
return unsafeBitCast(type, to: Type<U>.self)
}
class A {}
class B: A {}
let typeB = Type<B>()
// downcast Type<B> to Type<A>
let downcast: Type<A>? = asOptionalCast(type: typeB)
(downcast! == typeB) == true
// cast Type<A> (which here is Type<B>) back to Type<B>
let upcast: Type<B>? = asOptionalCast(type: downcast!)
(upcast! == Type<B>()) == true
</code></pre>
<p>The good part here that the hash value of the casted type won’t
change and testing against a new instance of the same dynamic type
will be always true.</p>
</div>
<div>
<div style="font-family:Helvetica,Arial;font-size:13px;color:rgba(0,0,0,1.0);margin:0px;line-height:auto">
<span class=""><br></span></div>
<span class=""><br></span>
<div>
<div style="font-family:helvetica,arial;font-size:13px">
<span class="">-- <br>
Adrian Zubarev<br>
Sent with Airmail</span></div>
</div>
<span class=""><br></span>
<div>
<div class="h5">
<p>Am 13. Juli 2016 um 20:31:22, Anton Zhilin (<a href="mailto:antonyzhilin@gmail.com" target="_blank">antonyzhilin@gmail.com</a>) schrieb:</p>
<blockquote type="cite">
<div>
<div>
<div dir="ltr">
<div class="gmail_extra">
<div class="gmail_quote"><span>2016-07-13 20:19 GMT+03:00 Adrian
Zubarev via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span>:</span>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
<div style="word-wrap:break-word">
<div>
<p><span>Am 13. Juli 2016 um 18:30:53, Anton Zhilin (<a href="mailto:antonyzhilin@gmail.com" target="_blank">antonyzhilin@gmail.com</a>) schrieb:</span></p>
<div>
<blockquote type="cite" style="margin:15px 0px;font-family:Helvetica,Arial;font-size:13px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">
<div>
<div class="gmail_quote" style="color:rgb(0,0,0);font-family:"helvetica Neue",helvetica;font-size:14px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">
<div><span>I see model of<span> </span><font face="monospace, monospace">Type<T></font><span> </span>as
follows:</span></div>
<div>
<ol style="margin:15px 0px">
<li style="margin:15px 0px">Values of<span> </span><font face="monospace, monospace">Type<T></font><span> </span>are
identifiers of types (8 bytes, I guess)<br></li>
<li style="margin:15px 0px">All used identifiers are contained
in<span> </span><font face="monospace, monospace">Type<Any></font><br></li>
<li style="margin:15px 0px"><font face="monospace, monospace">Type<T></font><span> </span>contains
a subset of those identifiers</li>
</ol>
</div>
</div>
</div>
</blockquote>
</div>
<p>I can’t follow your though here. Is this a totally new
behavior?</p>
</div>
</div>
</blockquote>
<div>
<div>That's how it works right now:</div>
<div><font face="monospace, monospace"><br></font></div>
<div><font face="monospace, monospace">sizeofValue(Bool.self)
//=> 0
(optimized away)<br></font></div>
<div><font face="monospace, monospace">sizeofValue(Bool.self as
Any.self) //=> 8</font><br></div>
<div><br></div>
<div>I'll put my thoughts another way:</div>
</div>
<div>
<ul>
<li>Internally, <font face="monospace, monospace">Type<T></font> contains an <font face="monospace, monospace">Int</font>, i.e. identifer of a type. For
every type, compiler must choose a different identifier</li>
<li>API of <font face="monospace, monospace">Type<T></font>
is defined so that it can only contain identifiers of subtypes of
<font face="monospace, monospace">T</font></li>
<li>For example, when you get a variable of <font face="monospace, monospace">Type<BaseClass></font>, it can
correspond to <font face="monospace, monospace">BaseClass</font><font face="arial, helvetica, sans-serif"> </font>or<font face="arial, helvetica, sans-serif"> </font><font face="monospace, monospace">DerivedClass</font></li>
</ul>
</div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
<div style="word-wrap:break-word">
<div>
<div>
<div>
<div>
<blockquote type="cite" style="margin:15px 0px;font-family:Helvetica,Arial;font-size:13px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">
<div>
<div class="gmail_quote" style="color:rgb(0,0,0);font-family:"helvetica Neue",helvetica;font-size:14px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">
<div>
<ol style="margin:15px 0px">
<li style="margin:15px 0px"><span>Upcasting uses
constructor<span> </span><font face="monospace, monospace">init<U: T>(upcasting:
Type<U>)</font></span></li>
</ol>
</div>
</div>
</div>
</blockquote>
</div>
<p>It does look neat but I can’t implement it. At least I have no
clue how to solve the problem that `T` is not a protocol or a class
type.</p>
</div>
</div>
</div>
</div>
</blockquote>
<div>We should add implicit convertion of <font face="monospace, monospace">Type<U></font> to <font face="monospace, monospace">Type<T></font><font face="arial, helvetica, sans-serif">, dulicating current behaviour
of</font> <font face="monospace, monospace">`as
T.Type`</font><font face="arial, helvetica, sans-serif">.</font></div>
<div><font face="arial, helvetica, sans-serif">That constructor
still can be useful, but it will be failable and not that
significant in practise.</font></div>
<div><font face="arial, helvetica, sans-serif">I don't like adding
compiler magic, but I guess we really should in this case, because
otherwise</font> <font face="monospace, monospace">Type<T></font> <font face="arial, helvetica, sans-serif">can't replace</font> <font face="monospace, monospace">T.Type</font><font face="arial, helvetica, sans-serif">.</font></div>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
<div style="word-wrap:break-word">
<div>
<div>
<div>
<div>
<div>
<div>
<blockquote type="cite" style="margin:15px 0px;font-family:Helvetica,Arial;font-size:13px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">
<div>
<div class="gmail_quote" style="color:rgb(0,0,0);font-family:"helvetica Neue",helvetica;font-size:14px;font-style:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">
<div>
<ol style="margin:15px 0px">
<li style="margin:15px 0px"><span>Size
of<span> </span><font face="monospace, monospace">Type<Type<T>></font><span> </span>is
8, static size of<span> </span><font face="monospace, monospace">Type<SomeProtocol></font><span> </span>is
0</span></li>
</ol>
</div>
</div>
</div>
</blockquote>
</div>
<p>I feel like you mean something different here than this:</p>
<p>```swift</p>
<p style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo">
<span>protocol</span> <span>SomeProtocol {}</span></p>
<p style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">
<br></p>
<p style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo">
<span>sizeof</span><span>(</span><span>SomeProtocol</span><span>.</span><span>self</span><span>)
==</span> <span>40</span></p>
<p style="margin-top:0px;margin-bottom:0px;font-size:11px;line-height:normal;font-family:Menlo">
<span>```</span></p>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
<div><br></div>
<div>That was a mistake. Static size of <font face="monospace, monospace">Type<SomeProtocol></font> will be 40,
and size property of its values can be less or greater than 40,
depending on internal type identifier.<br></div>
</div>
<br></div>
</div>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</div>
<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote>
</div>
<br></div>
</div></div></span></blockquote></div><div class="bloop_markdown"><p></p></div></body></html>