[swift-evolution] [Review] SE-0101: Rename sizeof and related functions to comply with API Guidelines
David Sweeris
davesweeris at mac.com
Wed Jun 29 18:50:05 CDT 2016
> On Jun 29, 2016, at 4:59 PM, Xiaodi Wu <xiaodi.wu at gmail.com> wrote:
>
> There must be a typo in these examples. `MemoryLayout(x.dynamicType).size` perhaps?
Dunno if it’s intentional or not. Hooman Mehr already suggested we add instance properties:
> On Jun 28, 2016, at 8:27 PM, Hooman Mehr via swift-evolution <swift-evolution at swift.org> wrote:
>
> How about we get rid of dynamicType by adding instance level properties as well:
>
> public struct MemoryLayout<T> {
>
> public static var size: Int { return sizeof(T) }
> public static var interval: Int { return strideof(T) }
> public static var alignment: Int { return alignof(T) }
>
> public var size: Int { return sizeof(T) }
> public var interval: Int { return strideof(T) }
> public var alignment: Int { return alignof(T) }
>
> init(_ : @autoclosure () -> T) {}
> }
>
> print(MemoryLayout<Int>.size) // 8
> print(MemoryLayout<Int>.interval) // 8
> print(MemoryLayout<Int>.alignment) // 8
>
> let x = 8
>
> print(MemoryLayout(x).size) // 8
> print(MemoryLayout(x).interval) // 8
> print(MemoryLayout(x).alignment) // 8
Doing that makes that list look like this:
MemoryLayout<Int8>.size // 1, correct
MemoryLayout(Int8.self).size // 1, almost certainly correct
MemoryLayout(Int8).size // 1, almost certainly correct
MemoryLayout(0 as Int8).size // 1, correct
MemoryLayout<Int8.Type>.size // 8, correct
MemoryLayout(Int8.Type.self).size // 8, correct, but is oddly worded
MemoryLayout(Int8.Type).size // 8, correct
I thought of something else… if we had a way to intentionally trigger a compile-time error in code that’s otherwise correct, we could define the `init(_: T.self)` function like this:
extension MemoryLayout {
public init(_ : T.Type) { #throwErrorIfReached("Incorrect Usage... NOCOMPILINGFORYOU!!!") }
}
Then the list of statements which compile would be much simpler:
let x: Int8 = 0
MemoryLayout<Int8>.size // 1, correct and unambiguous
MemoryLayout(x).size // 1, correct and unambiguous (assuming the instance properties are added)
MemoryLayout<Int8.Type>.size // 8, correct and unambiguous
But that’s its own proposal, and one I’m not sure is worth making.
- Dave Sweeris
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160629/f5af2586/attachment.html>
More information about the swift-evolution
mailing list