[Proposal] Synthesizing Equatable/Hashable conformance for enums and structs

Now that Swift 5 is taking proposals, I'm dusting off my proposal to
synthesize Equatable/Hashable conformance for enums and structs. I had
implemented this a few months ago hoping to squeeze it in by the Swift 4
deadline, but unfortunately the timeline was too tight.

The pull request for the proposal is here:
https://github.com/apple/swift-evolution/pull/706\. (Direct link to proposal
text:
swift-evolution/NNNN-synthesize-equatable-hashable.md at b3dcffc2e6f74e17eba05a6eb7eb29ad58bf36a3 · allevato/swift-evolution · GitHub
)

The pull request for the implementation (rebased last night) is here:
https://github.com/apple/swift/pull/9619

Thanks all!

Hi Tony. I’m really glad to see you’re brining this proposal back right away. It was a shame that it missed the deadline for Swift 4 and will be awesome to have it accepted for Swift 5.

···

On Aug 9, 2017, at 10:36 AM, Tony Allevato via swift-evolution <swift-evolution@swift.org> wrote:

Now that Swift 5 is taking proposals, I'm dusting off my proposal to synthesize Equatable/Hashable conformance for enums and structs. I had implemented this a few months ago hoping to squeeze it in by the Swift 4 deadline, but unfortunately the timeline was too tight.

The pull request for the proposal is here: https://github.com/apple/swift-evolution/pull/706\. (Direct link to proposal text: https://github.com/allevato/swift-evolution/blob/b3dcffc2e6f74e17eba05a6eb7eb29ad58bf36a3/proposals/NNNN-synthesize-equatable-hashable.md\)

The pull request for the implementation (rebased last night) is here: Synthesize Equatable/Hashable for complex enums, structs by allevato · Pull Request #9619 · apple/swift · GitHub

Thanks all!

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Looks good to me, though I have some clarifying questions. For a type which
conforms to both Equatable and Hashable:

1. To automatically derive both Equatable and Hashable, will it be
sufficient to declare “struct Foo: Hashable” (since Hashable refines
Equatable) or must “Equatable” also be listed?

2. Will it be possible to automatically derive “==” while manually
implementing “hashValue”?
2a. If so, will “Equatable” and “Hashable” both need to appear in the
declaration, or will “Hashable” alone suffice?

3. Will it be possible to automatically derive “hashValue” while manually
implementing “==”?
3a. If so, will “Equatable” and “Hashable” both need to appear in the
declaration, or will “Hashable” alone suffice?

Thanks,

Nevin

···

On Wed, Aug 9, 2017 at 11:36 AM, Tony Allevato via swift-evolution < swift-evolution@swift.org> wrote:

Now that Swift 5 is taking proposals, I'm dusting off my proposal to
synthesize Equatable/Hashable conformance for enums and structs. I had
implemented this a few months ago hoping to squeeze it in by the Swift 4
deadline, but unfortunately the timeline was too tight.

The pull request for the proposal is here: https://github.com/
apple/swift-evolution/pull/706. (Direct link to proposal text:
https://github.com/allevato/swift-evolution/blob/
b3dcffc2e6f74e17eba05a6eb7eb29ad58bf36a3/proposals/NNNN-
synthesize-equatable-hashable.md)

The pull request for the implementation (rebased last night) is here:
Synthesize Equatable/Hashable for complex enums, structs by allevato · Pull Request #9619 · apple/swift · GitHub

Thanks all!

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Thanks for the questions!

Looks good to me, though I have some clarifying questions. For a type
which conforms to both Equatable and Hashable:

1. To automatically derive both Equatable and Hashable, will it be
sufficient to declare “struct Foo: Hashable” (since Hashable refines
Equatable) or must “Equatable” also be listed?

Yes, because Hashable implies Equatable. (This is also the case with
Codable, where conforming to Codable synthesizes both Decodable and
Encodable.)

Implementation-wise, the compiler asks "does this type conform to X", which
is a distinct question from "does this type explicitly state X in its
protocol conformance list". The former is the correct question to always
ask, IMO—I don't believe there are any places in the compiler where the
latter would produce different behavior than the former (but someone please
correct me if I'm wrong).

2. Will it be possible to automatically derive “==” while manually
implementing “hashValue”?
2a. If so, will “Equatable” and “Hashable” both need to appear in the
declaration, or will “Hashable” alone suffice?

3. Will it be possible to automatically derive “hashValue” while manually
implementing “==”?
3a. If so, will “Equatable” and “Hashable” both need to appear in the
declaration, or will “Hashable” alone suffice?

Either or both members can be replaced with manual implementations—of
course it's up to the user at that point to keep those implementations
behaviorally consistent. Doing so does not change the behavior described
above regarding the protocol conformance list.

···

On Wed, Aug 9, 2017 at 9:18 AM Nevin Brackett-Rozinsky < nevin.brackettrozinsky@gmail.com> wrote:

Thanks,

Nevin

On Wed, Aug 9, 2017 at 11:36 AM, Tony Allevato via swift-evolution < > swift-evolution@swift.org> wrote:

Now that Swift 5 is taking proposals, I'm dusting off my proposal to
synthesize Equatable/Hashable conformance for enums and structs. I had
implemented this a few months ago hoping to squeeze it in by the Swift 4
deadline, but unfortunately the timeline was too tight.

The pull request for the proposal is here:
https://github.com/apple/swift-evolution/pull/706\. (Direct link to
proposal text:
https://github.com/allevato/swift-evolution/blob/b3dcffc2e6f74e17eba05a6eb7eb29ad58bf36a3/proposals/NNNN-synthesize-equatable-hashable.md
)

The pull request for the implementation (rebased last night) is here:
https://github.com/apple/swift/pull/9619

Thanks all!

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution