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

Jordan Rose jordan_rose at apple.com
Tue Jul 12 19:33:10 CDT 2016


> On Jul 12, 2016, at 17:03, Dave Abrahams <dabrahams at apple.com> wrote:
> 
> 
> on Tue Jul 12 2016, Jordan Rose <jordan_rose-AT-apple.com <http://at-apple.com/>> wrote:
> 
>>> On Jun 30, 2016, at 12:30, Dave Abrahams via swift-evolution <swift-evolution at swift.org> wrote:
>>> 
>>> 
>>> on Wed Jun 29 2016, Erica Sadun <erica-AT-ericasadun.com> wrote:
>>> 
>> 
>>>>> On Jun 29, 2016, at 3:59 PM, Xiaodi Wu via swift-evolution <swift-evolution at swift.org> wrote:
>>>>> 
>>>>> On Wed, Jun 29, 2016 at 4:50 PM, David Sweeris <davesweeris at mac.com <mailto:davesweeris at mac.com>> wrote:
>>>>> That’s the “as proposed” usage for getting the size of a value (from
>>>>> https://gist.github.com/erica/57a64163870486468180b8bab8a6294e
>>>> 
>>>>> <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.
>>>>> 
>>>>> There must be a typo in these examples. `MemoryLayout(x.dynamicType).size` perhaps?
>>>> 
>>>> I have listened. I have updated.
>>>> 
>>>> 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.of(x).size // 1
>>>> MemoryLayout.of(1).size // 8
>>>> MemoryLayout.of("hello").arraySpacing // 24
>>>> MemoryLayout.of(29.2).alignment // 8
>>> 
>>> I am still very skeptical that anyone needs the “Value” version, and as
>>> long as we're resyntaxing I am inclined to take it away and see how many
>>> people complain.  You can still always write it yourself.
>> 
>> Sorry to only come across this now. The proposed implementation does not work.
>> 
>> public static func of(_ candidate : @autoclosure () -> T) -> MemoryLayout<T>.Type {
>>  return MemoryLayout.init(candidate).dynamicType
>> }
>> 
>> let value: Any = 2 // dynamicType is Int
>> let layout = MemoryLayout(value).dynamicType // inlined from above
>> let arrayType = [value].dynamicType
>> 
>> ‘layout’ here is still 'MemoryLayout<Any>', not 'MemoryLayout<Int>',
>> for the same reason that ‘arrayType’ is ‘Array<Any>’ rather than
>> ‘Array<Int>’.
> 
> That is the right answer.  If you want to store an array of things with
> the same type as “value” (== Any) you need that MemoryLayout.
> 
>> If we want to support sizeofValue et al, we’d need to make these
>> instance properties rather than static properties, and may then want
>> to give up on the struct being generic.
> 
> That wouldn't change anything.  Nothing can possibly unwrap the Any and
> find out the size of the thing.  The current sizeofValue() doesn't work
> that way either:
> 
>  print(sizeofValue(1 as Int8 as Any))

Okay, I didn’t realize it was still looking for a static type. In that case there’s an even simpler implementation:

public static func of(_ candidate : @autoclosure () -> T) -> MemoryLayout<T>.Type {
  return MemoryLayout<T>.self
}

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


More information about the swift-evolution mailing list