[swift-evolution] MemoryLayout for a value

Dmitri Gribenko gribozavr at gmail.com
Sat Aug 6 16:06:22 CDT 2016


On Fri, Aug 5, 2016 at 7:35 PM, Boris Wang via swift-evolution
<swift-evolution at swift.org> wrote:
> codes in swift REPL:
>
> protocol P {
> var x:Int {get}
> }
> MemoryLayout<P>.size
> //r0 : Int = 40
>
> struct S1 {
> var v1:Int = 0
> }
> MemoryLayout<S1>.size
> //r1: Int =8
>
> struct S2: P {
> var v2: Int
> var x:Int
> }
> MemoryLayout <S2>.size
> //r2: Int = 16
>
> ** Question:
> Why we need to known the size of a object that can't be instanced?

The size of a variable of static type S2 is not equal to the size of
the variable of static type P, even though it can dynamically contain
an instance of S2.

Consider:

var x: P = S2(v2: 0, x: 0)

The size of x is not equal to the size of S2.

Dmitri

-- 
main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
(j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/


More information about the swift-evolution mailing list