<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, May 2, 2016 at 4:32 PM, Chris Lattner via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><span>On May 2, 2016, at 1:10 PM, Dave Abrahams via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br>
>>><br>
>>> I'd rather have<br>
>>><br>
>>> MemoryLayout<T>.size<br>
>>> MemoryLayout<T>.alignment<br>
>>> MemoryLayout<T>.spacing<br>
>><br>
>> This would be a legit use for 'extension Any'. IMO it'd be even better<br>
>> as just T.size, T.alignment, T.spacing.<br>
><br>
> I very much disagree. I don't want to see Array(1..<100).size == 24.<br>
<br>
</span>I agree with Dave here. Even if these got more verbose names (to avoid confusion with count), it is still unfortunate to pollute code completion for these uncommonly used "operators”. I’m coming around to agree with Dave’s view that dynamicType should be made into a global function for the same reason.<br></blockquote><div><br></div><div>This feedback has been very edifying. I've been playing around a little bit in the IBM Swift Sandbox (it's what I've got handy). I think Dave's solution is supremely elegant, but I'm not entirely comfortable proposing elimination of `sizeofValue(_:)` and friends; I've just got no real-world information about how useful they are or not, and the onus is on the person proposing the change to show why the change is necessary and appropriate. I wouldn't know where to begin.</div><div><br></div><div>Dave's suggestion can be bolted onto Swift 2 as follows:</div><div><br></div><div>```</div><div><div>struct MemoryLayout<T> {</div><div> static var alignment: Int {</div><div> return alignof(T)</div><div> }</div><div> static var size: Int {</div><div> return sizeof(T)</div><div> }</div><div> static var spacing: Int {</div><div> return strideof(T)</div><div> }</div><div> private init() { }</div><div>}</div></div><div>```</div><div><br></div><div>I think, though, that it becomes more confusing to include `sizeofValue(_:)` in this syntax, because it'd be possible to write `MemoryLayout<Base>.size(ofValue: Subclass())`, which is a can of worms. However, we can do this:</div><div><br></div><div>```</div><div><div>struct MemoryLayout {</div><div> static func alignment<T>(type: T.Type) -> Int {</div><div> return alignof(type)</div><div> }</div><div> static func size<T>(type: T.Type) -> Int {</div><div> return sizeof(type)</div><div> }</div><div> static func spacing<T>(type: T.Type) -> Int {</div><div> return strideof(type)</div><div> }</div><div> static func alignment<T>(value: T) -> Int {</div><div> return alignofValue(value)</div><div> }</div><div> static func size<T>(value: T) -> Int {</div><div> return sizeofValue(value)</div><div> }</div><div> static func spacing<T>(value: T) -> Int {</div><div> return strideofValue(value)</div><div> }</div><div> private init() { }</div><div>}</div></div><div>```</div><div><br></div><div>It's not up to Dave's standards of elegance, but it seems acceptable IMO. The preposition, incidentally, has to go because parsing rules (at least in Swift 2) mean `size(Int)` is fine but `size(of: Int)` blows up (it has to be `size(of: Int.self)`). Is there an issue with using overloaded methods instead of unique labels in replacing `sizeof(_:)` and `sizeofValue(_:)`?</div><div><br></div></div></div></div>