[swift-evolution] Does protocol support add to an object's size?
swift-evolution at haravikk.me
Fri Feb 17 05:05:23 CST 2017
> On 15 Feb 2017, at 17:51, Daryle Walker via swift-evolution <swift-evolution at swift.org> wrote:
> I don't know how protocol support works. I asking because I want to maintain the stride of an array being the total count times the stride of the element, which would complicate nominal arrays if adding protocols to one breaks that.
Short answer; no.
A protocol can't add stored properties, which are what determine the size/stride of a type; all they can add are computed properties and methods, neither of which are actually stored alongside the objects themselves.
Basically a type's size is memory required to store only the parts that are unique to that specific object (stored properties), all a protocol does is tell the compiler some operations you can perform, the actual code for which is stored elsewhere.
As Karl points out though the type's size isn't necessarily the full picture, as a store property can be a pointer to some other piece of memory such as an object (class, rather than struct) or to an unmanaged buffer and other constructs that are particularly useful for things like arrays.
Also, for future reference, questions like this are more appropriate to the swift-users mailing list; swift-evolution is intended more for language improvements, rather than general queries. Easy enough mistake to make though!
More information about the swift-evolution