[swift-evolution] [Discussion] Seal `T.Type` into `Type<T>`
Adrian Zubarev
adrian.zubarev at devandartist.com
Fri Jul 15 13:29:57 CDT 2016
Changed the implementation to this:
public var size: Int { return _sizeof(self._metatype) }
public var stride: Int { return _strideof(self._metatype) }
public var alignment: Int { return _alignof(self._metatype) }
public static var size: Int { return _sizeof(T.metatype) }
public static var stride: Int { return _strideof(T.metatype) }
public static var alignment: Int { return _alignof(T.metatype) }
Where the internal functions are based on current swift implementation except that instead of T.Type it would be better to use AnyMetatype.
internal func _sizeof(_ metatype: AnyMetatype) -> Int {
return Int(Builtin.sizeof(metatype))
}
internal func _strideof(_ metatype: AnyMetatype) -> Int {
return Int(Builtin.strideof_nonzero(metatype))
}
internal func _alignof(_ metatype: AnyMetatype) -> Int {
return Int(Builtin.alignof(metatype))
}
I can’t test this because current sizeof, strideof and alignof are using T.Type where something like this doesn’t work:
sizeof(Int.self) // 8
sizeof(CustomStringConvertible.self) // 40
sizeof(Int.self as CustomStringConvertible.Type) // Error
That said I can only assume that the buildin function will return the correct value for the provided metatype.
let x = Int // equals Type<Int>.sharedInstance
x.size // 8
let y = Type<CustomStringConvertible>.cast(x)
y?.size // should be 8 as well
size and alignment would work analogically.
Type<T>.size // always returns the size of T
--
Adrian Zubarev
Sent with Airmail
Am 15. Juli 2016 um 19:05:56, Anton Zhilin (antonyzhilin at gmail.com) schrieb:
Next, I’d like to remind you of my
size,
stride,
align concerns. Consider this example:
let x = Type<Int>()
let y = Type<CustomStringConvertible>(casting: x)!
x.size //=> 8
y.size //=> 8 or 40?
I think that
y.size should be 8, because we are dynamically checking size of particular instance
y, which refers to specific subtype of
CustomStringConvertible.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160715/c3b62c2e/attachment.html>
More information about the swift-evolution
mailing list