<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 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 class="HOEnZb"><div class="h5"><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">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 class="HOEnZb"><font color="#888888"><br></font></span></blockquote><div><br></div><div>Proposal and stdlib PRs have both been created.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="HOEnZb"><font color="#888888">
--<br>
-Dave<br>
</font></span></blockquote></div><br></div></div>