0

Synthesizing Equatable and Hashable conformance.

I would like to take a look at source code where it is implemented. But I can't find it at https://github.com/apple/swift/
Particularly I'm interested in:

we propose that a type synthesize conformance to Equatable/Hashable if all of its members are Equatable/Hashable

How do they check whether all of its members are Equatable/Hashable?

Actually I want to write my own extension. I want to synthesize Comparable conformance if there is only one member which is Comparable. Or to synthesize Equatable/Hashable conformance only based on Equatable/Hashable members. I think it will be pretty handy too.

Roman
  • 1,309
  • 14
  • 23
  • 2
    "I want to synthesize Comparable conformance if there is only one member which is Comparable" You do realise that would involve forking the swift compiler, right? – Sweeper Sep 14 '20 at 05:17
  • @Sweeper I don't want to fork the swift compiler. I think I'm not ready for that yet))) I just want to implement such a behaviour locally, for my own use. As an extension or so. – Roman Sep 14 '20 at 05:20
  • 2
    The proposal https://github.com/apple/swift-evolution/blob/master/proposals/0185-synthesize-equatable-hashable.md has a link to the implementation https://github.com/apple/swift/pull/9619. As you can see (and Sweeper said above), it is built into the compiler. – Martin R Sep 14 '20 at 05:22
  • 2
    Well, this whole "synthesising a protocol conformance" thing is compiler magic, so you would have to create your own version of the compiler in order to synthesise your own protocol conformances. The Swift language doesn't allow you to express such ideas. The relevant file of the compiler source code seems to be: https://github.com/apple/swift/blob/master/lib/Sema/DerivedConformanceEquatableHashable.cpp – Sweeper Sep 14 '20 at 05:24
  • 2
    So perhaps a more useful question to ask is, "how can I make my types implement `Comparable` if one of their properties is `Comparable`?" – Sweeper Sep 14 '20 at 05:27
  • Ok guys, thanks, I've got it. – Roman Sep 14 '20 at 05:32
  • @Sweeper Yes, such a question is interesting as well :) – Roman Sep 14 '20 at 05:33

0 Answers0