[swift-evolution] [Review] SE-0101: Rename sizeof and related functions to comply with API Guidelines

Dave Abrahams dabrahams at apple.com
Thu Jun 30 14:27:31 CDT 2016


on Wed Jun 29 2016, David Sweeris <davesweeris-AT-mac.com> wrote:

> (While I was typing this up, I realized that the exact usage you’re
> worried about, “MemoryLayout(Int.self).size” won’t compile, since
> `MemoryLayout` currently doesn’t have instance properties. If you’re
> worried about someone incorrectly typing out
> “MemoryLayout(Int.self).dynamicType.size”, though…)
>
> I made a rather critical typo in my earlier reply. It should’ve been
> “init(_: T.Type)”, instead of “init(_: T.self)”, which is to say this:
> extension MemoryLayout { // assuming `MemoryLayout<T>` is already defined as proposed
>     public init(_ : T.Type) {} // makes it so that `MemoryLayout(T.self)` still has the correct type for `T`
> }
>
> Here are the results of some quick playgrounding in the WWDC Xcode 8 beta:
> // without `init(_ : T.Type)` defined
> MemoryLayout<Int8>.size                       // 1, correct
> MemoryLayout(Int8.self).dynamicType.size      // 8, almost certainly wrong
> //MemoryLayout(Int8).dynamicType.size           // error
> MemoryLayout(0 as Int8).dynamicType.size      // 1, correct
> MemoryLayout<Int8.Type>.size                  // 8, correct
> MemoryLayout(Int8.Type.self).dynamicType.size // 8, correct, but is oddly worded
> //MemoryLayout(Int8.Type).dynamicType.size      // error
>
> // with `init(_ : T.Type)` defined
> MemoryLayout<Int8>.size                       // 1, correct
> MemoryLayout(Int8.self).dynamicType.size      // 1, almost certainly correct
> MemoryLayout(Int8).dynamicType.size           // 1, almost certainly correct
> MemoryLayout(0 as Int8).dynamicType.size      // 1, correct
> MemoryLayout<Int8.Type>.size                  // 8, correct
> MemoryLayout(Int8.Type.self).dynamicType.size // 8, correct, but is oddly worded
> MemoryLayout(Int8.Type).dynamicType.size      // 8, correct
>
> The only value that changes (aside from the errors) is the one “typo” that you were worried about.
>
> Do this change your mind? 

No; it's too tricky. In your design MemoryLayout(x) now has two
different meanings depending on the type of x.

-- 
Dave


More information about the swift-evolution mailing list