<div dir="ltr">On Wed, Aug 3, 2016 at 11:27 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:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class=""><br>
on Wed Aug 03 2016, Xiaodi Wu <<a href="http://xiaodi.wu-AT-gmail.com" rel="noreferrer" target="_blank">xiaodi.wu-AT-gmail.com</a>> wrote:<br>
<br>
> Why not just MemoryLayout.init(of instance: T), and drop the autoclosure<br>
> magic altogether?<br>
<br>
</span>My proposal *does* drop the autoclosure magic. `type(of: x)` is already in<br>
the standard library (replacing `x.dynamicType`). The reasons not to have:<br>
<br>
MemoryLayout(of: x)<br>
<br>
where x is an arbitrary instance, is that it reads and pronounces the<br>
same as<br>
<br>
MemoryLayout<X><br>
<br>
but has different meaning, and even a different type (which results in<br>
additional API complexity—the forwarding vars I showed in the [Aside]<br>
box from my previous post). Imagine explaining the difference between<br>
these two in that world:<br>
<br>
MemoryLayout<Int><br>
MemoryLayout(of: Int.self)<br>
<br>
The first is a type representing the layout of Int. The second is an<br>
instance of that type representing the layout of Int's metatype.<br>
<span class=""><br>
> The classic sizeofValue evaluated its argument, and in Foundation several<br>
> uses of it actually relied on that side effect. While autoclosures are<br>
> quite clever, in general I think the user expectation is that given<br>
> `a(b(c))` both a and b are invoked, side effects and all.<br>
><br>
> Note that both type(of:) as it's currently implemented and the old<br>
> dynamicType evaluate its argument/receiver.<br>
<br>
</span>I didn't realize that, but it's fine. I'm attached to the use of<br>
`type(of:)` in this idiom, not to having an autoclosure involved.<br></blockquote><div><br></div><div>Your proposed `.of(type(of: x)).size` differs from the original `sizeofValue()` in at least one other way--is this in fact *why* you're proposing the change? In the classic syntax:</div><div><br></div><div>```</div><div><div>protocol P { }</div><div>print(sizeof(P.self)) // 40</div><div><br></div><div>struct S : P {</div><div> let x = 2</div><div>}</div><div>print(sizeof(S.self)) // 8</div><div><br></div><div>var a: P = S()</div><div>print(a.dynamicType) // S</div><div>print(sizeofValue(a)) // 40</div></div><div>```</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class=""><div class="h5">
> No one afaik has ever thought that behavior to be anomalous (though I<br>
> bet we're about to hear some arguments to that effect now).<br>
><br>
> On Wed, Aug 3, 2016 at 15:46 Dave Abrahams via swift-evolution <<br>
> <a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br>
><br>
>><br>
>> Having seen the effects in the standard library and in other<br>
>> code, I'm concerned that we may have made a mistake in removing<br>
>> `sizeofValue` et al without providing a replacement. In the standard<br>
>> library, we ended up adding an underscored API that allows<br>
>><br>
>> MemoryLayout._ofInstance(someExpression).size<br>
>><br>
>> Where someExpression is an autoclosure, and thus not evaluated. I<br>
>> wanted to bring up the possibility of introducing a replacement as a<br>
>> bufix.<br>
>><br>
>> I propose that the way to express the above should be:<br>
>><br>
>> MemoryLayout.of(type(of: someExpression)).size<br>
>><br>
>> implementable as:<br>
>><br>
>> extension MemoryLayout {<br>
>> @_transparent<br>
>> public<br>
>> static func of(_: T.Type) -> MemoryLayout<T>.Type {<br>
>> return MemoryLayout<T>.self<br>
>> }<br>
>> }<br>
>><br>
>> I think this API would solve the concerns I had about confusability that<br>
>> led me to advocate dropping the ability to ask for the size of a value.<br>
>> The only way to use it is to pass a type and these two expressions have<br>
>> equivalent meaning:<br>
>><br>
>> MemoryLayout<Int><br>
>> MemoryLayout.of(Int.self)<br>
>><br>
>> It also has the benefit of isolating the autoclosure magic to type(of:).<br>
>><br>
>> ,----[ Aside ]<br>
>> | A slightly cleaner use site is possible with a larger API change:<br>
>> |<br>
>> | MemoryLayout(type(of: someExpression)).size<br>
>> |<br>
>> | Which would involve changing MemoryLayout from an `enum` to<br>
>> | a `struct` and adding the following:<br>
>> |<br>
>> | extension MemoryLayout {<br>
>> | public init(_: T.Type) {}<br>
>> |<br>
>> | public var size: Int { return MemoryLayout.size }<br>
>> | public var stride: Int { return MemoryLayout.stride }<br>
>> | public var alignment: Int { return MemoryLayout.alignment }<br>
>> | }<br>
>> |<br>
>> | However I am concerned that dropping ".of" at the use site is worth the<br>
>> | added API complexity.<br>
>> `----<br>
>><br>
>> Thoughts?<br>
>> --<br>
>> -Dave<br>
>><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>
<br>
</div></div><span class=""><font color="#888888">--<br>
-Dave<br>
</font></span></blockquote></div><br></div></div>