[swift-evolution] Trial balloon: conforming sizeof, sizeofValue, etc. to naming guidelines

Xiaodi Wu xiaodi.wu at gmail.com
Mon May 2 18:48:14 CDT 2016

On Mon, May 2, 2016 at 4:32 PM, Chris Lattner via swift-evolution <
swift-evolution at swift.org> wrote:

> On May 2, 2016, at 1:10 PM, Dave Abrahams via swift-evolution <
> swift-evolution at swift.org> wrote:
> >>>
> >>> I'd rather have
> >>>
> >>> MemoryLayout<T>.size
> >>> MemoryLayout<T>.alignment
> >>> MemoryLayout<T>.spacing
> >>
> >> This would be a legit use for 'extension Any'. IMO it'd be even better
> >> as just T.size, T.alignment, T.spacing.
> >
> > I very much disagree.  I don't want to see Array(1..<100).size == 24.
> 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.

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.

Dave's suggestion can be bolted onto Swift 2 as follows:

struct MemoryLayout<T> {
    static var alignment: Int {
        return alignof(T)
    static var size: Int {
        return sizeof(T)
    static var spacing: Int {
        return strideof(T)
    private init() { }

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:

struct MemoryLayout {
    static func alignment<T>(type: T.Type) -> Int {
        return alignof(type)
    static func size<T>(type: T.Type) -> Int {
        return sizeof(type)
    static func spacing<T>(type: T.Type) -> Int {
        return strideof(type)
    static func alignment<T>(value: T) -> Int {
        return alignofValue(value)
    static func size<T>(value: T) -> Int {
        return sizeofValue(value)
    static func spacing<T>(value: T) -> Int {
        return strideofValue(value)
    private init() { }

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(_:)`?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160502/13d55fb3/attachment.html>

More information about the swift-evolution mailing list