codes in swift REPL:<br><br>protocol P {<br> var x:Int {get}<br>}<br>MemoryLayout<P>.size <br>//r0 : Int = 40<br><br>struct S1 {<br> var v1:Int = 0<br>}<br>MemoryLayout<S1>.size<br>//r1: Int =8<br><br>struct S2: P {<br> var v2: Int<br> var x:Int<br>}<br>MemoryLayout <S2>.size<br>//r2: Int = 16<br><br>** Question:<br> Why we need to known the size of a object that can't be instanced?<br><br> ** Confuse:<br>MemoryLayout <S2.Type>.size<br>//r3: Int = 0<br><br>MemoryLayout <P.Type>.size<br>//r4: Int = 16<br><br><br><br><div class="gmail_quote"><div dir="ltr">Xiaodi Wu via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>>于2016年8月5日 周五16:34写道:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">On Thu, Aug 4, 2016 at 6:02 PM, Dave Abrahams <span dir="ltr"><<a href="mailto:dabrahams@apple.com" target="_blank">dabrahams@apple.com</a>></span> wrote:<br></div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><br>
on Thu Aug 04 2016, Dmitri Gribenko <gribozavr-AT-gmail.com> wrote:<br>
<br>
> On Wed, Aug 3, 2016 at 7:28 PM, Xiaodi Wu via swift-evolution<br>
> <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br>
>> Could I suggest an alternative? It's conservative in that it mimics the<br>
>> relationships we had before the proposal was implemented and also maintains<br>
>> the simplicity of the caseless enum:<br>
>><br>
>> ```<br>
>> extension MemoryLayout {<br>
>> static func size(ofValue _: T) -> Int { return MemoryLayout.size }<br>
>> // etc.<br>
>> }<br>
>> ```<br>
><br>
> I like this API. I think given all the alternatives that we explored,<br>
> it is better than those. I also think that it nicely avoids the<br>
> following issue with the proposed MemoryLayout.of(type(of:<br>
> someExpression)).size syntax.<br>
><br>
> Imagine that you have a value whose static type differs from the<br>
> dynamic type. For example, a protocol existential:<br>
><br>
> protocol P {}<br>
> extension Int : P {}<br>
> var x: P = 10<br>
><br>
> The question is, what does MemoryLayout.of(type(of: x)).size compute,<br>
> size of the existential box, or the size of an Int instance? The<br>
> semantics of 'type(of:)' are "return the dynamic type", so the<br>
> straightforward conclusion is that MemoryLayout.of(type(of: x)).size<br>
> returns the size of the dynamic type instance, of Int.<br>
><br>
> What actually happens is that 'type(of: x)' returns a dynamic value of<br>
> 'Int.self', statically typed as 'P.Type'. So P gets deduced for the<br>
> generic parameter of MemoryLayout, and MemoryLayout.of(type(of:<br>
> x)).size returns the size of the protocol box.<br>
><br>
> I think due to this complex interaction, using type(of:) might lead to<br>
> confusing code, and thus I like Xiaodi's approach better.<br>
><br>
> Dmitri<br>
<br>
</div></div>Okay, I'm convinced; that's what we should do.<span><font color="#888888"><br></font></span></blockquote><div><br></div></div></div></div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>Proposal and stdlib PRs have both been created.</div></div></div></div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span><font color="#888888">
--<br>
-Dave<br>
</font></span></blockquote></div></div></div>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">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>
</blockquote></div>