That’s the “as proposed” usage for getting the size of a value (from
https://gist.github.com/erica/57a64163870486468180b8bab8a6294e)
// Types
MemoryLayout<Int>.size // 8
MemoryLayout<Int>.arraySpacing // 8
MemoryLayout<Int>.alignment // 8
// Value
let x: UInt8 = 5
MemoryLayout(x).dynamicType.size // 1
MemoryLayout("hello").dynamicType.arraySpacing // 24
MemoryLayout(29.2).dynamicType.alignment // 8
At least, I thought that was the latest version of the proposal. Maybe I’ve
gotten confused.
- Dave Sweeris
> On Jun 29, 2016, at 4:36 PM, Xiaodi Wu <[email protected]> wrote:
>
>
>
> On Wed, Jun 29, 2016 at 4:29 PM, David Sweeris via swift-evolution
> <[email protected] <mailto:[email protected]>> 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?
>
> These examples make no sense to me. Why are we asking for the size of the
> dynamicType of MemoryLayout?
>
>
>
>
> On Jun 29, 2016, at 12:34 PM, Dave Abrahams <[email protected]
> <mailto:[email protected]>> wrote:
>>
>>
>> on Wed Jun 29 2016, David Sweeris <davesweeris-AT-mac.com
>> <http://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
>>>> <[email protected] <mailto:[email protected]>> 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
>
>
> _______________________________________________
> swift-evolution mailing list
> [email protected] <mailto:[email protected]>
> https://lists.swift.org/mailman/listinfo/swift-evolution
> <https://lists.swift.org/mailman/listinfo/swift-evolution>
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution