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

David Sweeris davesweeris at mac.com
Wed Jun 29 16:29:26 CDT 2016


(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? 



On Jun 29, 2016, at 12:34 PM, Dave Abrahams <dabrahams at apple.com> wrote:
> 
> 
> on Wed Jun 29 2016, David Sweeris <davesweeris-AT-mac.com> wrote:
> 
>> Would adding a "init(_: T.self) {...}" solve that issue?
>> 
> 
> ? I don't see how.
> 
>> 
>> Sent from my iPhone
>> 
>>> On Jun 29, 2016, at 01:54, Dave Abrahams via swift-evolution <swift-evolution at swift.org> wrote:
>>> 
>>> My worry is that people will write 
>>> 
>>> MemoryLayout(Int.self).size 
>>> 
>>> when they mean
>>> 
>>> MemoryLayout<Int>.size 
>>> 
>>> (often because for some reason they don't like angle brackets).
>>> 
>>> I prefer to make the uncommon case much harder to write.
> 
> -- 
> Dave


- Dave Sweeris

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160629/fda000f6/attachment.html>


More information about the swift-evolution mailing list