In apple's implementation of Identifiable
the required function is
associatedtype ID
var id: Self.ID { get }
why is self required? What is the difference between that and
associatedtype ID
var id: ID { get }
In apple's implementation of Identifiable
the required function is
associatedtype ID
var id: Self.ID { get }
why is self required? What is the difference between that and
associatedtype ID
var id: ID { get }
Copyright © 2021 Jogjafile Inc.
I think this is just a convention Apple uses when writing their documentation. They write all their associated types with the
Self.prefix. Examples: 1, 2, 3. This is so that it is clear what is an associated type, and what isn't. If the declaration forIdentifiable.idhas instead been written like:To someone who's never used SwiftUI before, it is unclear whether
IDis an entirely separate, top-level type, or an associated type. If it saidSelf.ID, however, it is clear thatIDis an associated type of a protocol.Other than that, the
Self.prefix does not mean anything special. It's just like theself.prefix when referring to a instance member. It is optional in most cases. Just like theself.prefix, you probably needSelf.to resolve name conflicts in some situations, though I can't think of any right now.Apple also seems to have a convention for nested types - to always write out
OuterTypeName.InnerTypeName, when justInnerTypeNameis enough. Example: