[swift-evolution] [Pitch] Renaming sizeof, sizeofValue, strideof, strideofValue

Erica Sadun erica at ericasadun.com
Thu Jun 2 00:27:16 CDT 2016


Using MemoryLayout is a much bigger change than the simple name changes being pitched here. I'm not ruling it out, but it may be deferred or additive as the 3.0 timeline allows and the core team decides.

For now, my suggested design (strongly based on Wux's feedback and the previous thread discussion) is:

/// Returns the contiguous memory footprint of `T`.
///
/// Does not include any dynamically-allocated or "remote" storage.
/// In particular, `size(of: X.self)`, when `X` is a class type, is the
/// same regardless of how many stored properties `X` has.
public func size<T>(of: T.Type) -> Int

/// Returns the contiguous memory footprint of  `T`.
///
/// Does not include any dynamically-allocated or "remote" storage.
/// In particular, `size(of: a)`, when `a` is a class instance, is the
/// same regardless of how many stored properties `a` has.
public func size<T>(ofValue: T) -> Int

/// Returns the least possible interval between distinct instances of
/// `T` in memory.  The result is always positive.
public func spacing<T>(of: T.Type) -> Int

/// Returns the least possible interval between distinct instances of
/// `T` in memory.  The result is always positive.
public func spacing<T>(ofValue: T) -> Int

/// Returns the minimum memory alignment of `T`.
public func align<T>(of: T.Type) -> Int

/// Returns the minimum memory alignment of `T`.
public func align<T>(ofValue: T) -> Int


> On Jun 1, 2016, at 11:24 PM, Patrick Smith <pgwsmith at gmail.com> wrote:
> 
> I really like this idea. This IMO is lower level functionality than `type(of:)` (née dynamicType), so I think it makes sense for it to be grouped under its own domain, the MemoryLayout type.
> 
> Plus MemoryLayout can be extended with new convenience methods.
> 
> I’m fine with those old methods being removed, but I never use them so! Is it the same as calling type(of:) then using that with MemoryLayout? I imagine they could be fixit’d easily, and that they compile down to the same underlying code.
> 
> Patrick
> 
>> On 2 Jun 2016, at 3:05 PM, Xiaodi Wu via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> 
>> 2. Dave A. and others expressed the opinion that these should probably not be global functions; his preference was for:
>> 
>> ```
>> MemoryLayout<T>.size // currently sizeof()
>> MemoryLayout<T>.spacing // currently strideof()
>> MemoryLayout<T>.alignment // currently alignof()
>> ```
>> 
>> 3. Dave A. proposed that sizeofValue(), strideofValue(), and alignofValue() are better off removed altogether. I don't know if people are going to be happy about this idea.
> 

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


More information about the swift-evolution mailing list