Many types in Swift, namely value types, benefit from the ability to automatically conform to certain protocols if they satisfy certain conditions. For example, if an enum in Swift either has no associated values or has all Equatable associated values, then it can get an automatic Equatable conformance (e.g. a synthesized static func == (lhs:rhs:)) just by marking the enum with the Equatable protocol conformance. Same for Hashable and various other protocol conformances (and the same is true for structs as well).
For example:
enum SomeEnum: Equatable {
case a(SomeEquatableStruct)
case b
}
The above has no need for a manual static func == (lhs: Self, rhs: Self) to be written. However, even though adding an Identifiable conformance for enum types is trivial*, marking an enum as Identifiable still requires that a manual id property be written out. I'm curious if there would be some interest in changing this through an SE proposal to allow for automatically synthesizing an id property for enums marked with Identifiable.
* For enums with no associated values, simply using Self is enough for the ID type. For enums with associated values there is a bit more complexity: the enum type itself must be marked as Hashable (and can use the auto-synthesized conformance) and each associated value must also therefore be Hashable) before Self can be used as the ID type.
Examples for both an enum with no associated values and an enum with associated values is shown below:
enum SomeEnum: Identifiable {
case a
case b
// The discussion is about if the following
// should be automatically synthesized
var id: Self { self }
}
struct SomeStructA: Hashable { ... }
struct SomeStructB: Hashable { ... }
enum SomeEnum: Hashable, Identifiable {
case a(SomeStructA)
case b(SomeStructB)
case c
// The discussion is about if the following
// should be automatically synthesized
var id: Self { self }
}
I would welcome any thoughts and discussion about this. ![]()
EDIT 1: I forgot to mention that AnyObject does have an extension that adds an automatic Identifiable conformance based on its ObjectIdentifier. While not quite the same as the automatic synthesis I describe above, it does show that there's a bit of precedence for the idea.
EDIT 2: After more discussion, it is clear that a simple Hashable requirement for associated values is not enough. Instead, associated values themselves should be Identifiable in a similar vein to how synthesizing Equatable or Hashable conformances requires that their properties are also Equatable or Hashable, respectively. There is some discussion about if/how enum cases with multiple associated values would need to handle Identifiable conformance: with the most straightforward requirement being that each case would need to have either no associated values or each associated value conform to Identifiable (where the Identifiable conformance would be a combination of the case "name"/"tag" and (if existing) each associated values' Identifiable id). For example:
struct SomeStructA: Identifiable { ... }
struct SomeStructB: Identifiable { ... }
// The discussion is about if the following conformance
// should be automatically synthesized for each case
enum SomeEnum: Identifiable {
case a(SomeStructA) // Would use the `a` name/tag and `SomeStructA`'s `Identifiable` `id`
case b(SomeStructB) // Would use the `b` name/tag and `SomeStructB`'s `Identifiable` `id`
case c // Would only need to use `c`'s name/tag
}