Why is it necessary to refer to self for the Identifiable protocol?

erotsppa

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 }
Sweeper

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 for Identifiable.id has instead been written like:

var id: ID { get }

To someone who's never used SwiftUI before, it is unclear whether ID is an entirely separate, top-level type, or an associated type. If it said Self.ID, however, it is clear that ID is an associated type of a protocol.

Other than that, the Self. prefix does not mean anything special. It's just like the self. prefix when referring to a instance member. It is optional in most cases. Just like the self. prefix, you probably need Self. 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 just InnerTypeName is enough. Example:

var keyDecodingStrategy: JSONDecoder.KeyDecodingStrategy

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why is casting in equals necessary?

Why is JavaScript bind() necessary?

Protocol func returning Self

How to confirm an enumeration to Identifiable protocol in Swift?

Why does self in "class << self" refer to the class?

Why is it necessary to place self as a parameter for method decorators?

Why a cast is necessary in this case?

Refer to self in OpenAPI 3.0

Why isn't self passed once an attribute is changed to refer to an external function?

Why does Swift disallow assignment to self in class init, but not in protocol init?

Refer to 'self' in a static context

Identifiable protocol in swiftUI... id property... var vs let

Why is dynamic binding necessary?

SwiftUI - Custom Identifier with Identifiable Protocol

Self in protocol

Is parameter in self executing function necessary?

Protocol with property of type Self can only be used as generic constraint, why?

Why its necessary to use any string with dot point with self in class python(self.x=x)?

Why it is not required for ViewController class to conform to the NSObject Protocol while necessary for other classes

"self as?" within protocol extension

Why is Angular Universal necessary?

Using self in function in a protocol

Protocol oriented Programming Swift - Identifiable

why are these Jackson annotations necessary?

tkinter: Why is "self._w" used to refer to a widget and not "self" in the "tk.call" method?

Swift enum conformance to identifiable: Type doesn't conform to Identifiable protocol

Inheritance: why does self refer to child class?

Using SwiftUI ForEach to iterate over [any Protocol] where said protocol is Identifiable

Is &self parameter necessary in the function of trait?