Do you means a array of protocol P, rather than a array of element conform with P?<br><div class="gmail_quote"><div dir="ltr">Dave Abrahams via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>>于2016年8月6日 周六13:22写道:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
on Fri Aug 05 2016, Boris Wang <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br>
<br>
> Addition:<br>
><br>
> I think protocol should't has size, or it's size should be zero.<br>
> Because you can't put the 40 bytes data in anywhere.<br>
<br>
You certainly can. If protocols didn't have a size, Array wouldn't be<br>
able to store them.<br>
<br>
><br>
><br>
> Boris Wang <<a href="mailto:kona.ming@gmail.com" target="_blank">kona.ming@gmail.com</a>>于2016年8月6日 周六10:35写道:<br>
><br>
>> 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>
>> Xiaodi Wu via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>>于2016年8月5日<br>
>> 周五16:34写道:<br>
>><br>
>>> On Thu, Aug 4, 2016 at 6:02 PM, Dave Abrahams <<a href="mailto:dabrahams@apple.com" target="_blank">dabrahams@apple.com</a>><br>
>>> wrote:<br>
>>><br>
>>>><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<br>
>>>> the<br>
>>>> >> relationships we had before the proposal was implemented and also<br>
>>>> 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>
>>>> Okay, I'm convinced; that's what we should do.<br>
>>>><br>
>>><br>
>>> Proposal and stdlib PRs have both been created.<br>
>>><br>
>>><br>
>>>> --<br>
>>>> -Dave<br>
>>>><br>
>>> _______________________________________________<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>
>>><br>
>><br>
> _______________________________________________<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>
><br>
<br>
--<br>
-Dave<br>
<br>
_______________________________________________<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>