XcodeEquatableGenerator icon indicating copy to clipboard operation
XcodeEquatableGenerator copied to clipboard

Hashable instead?

Open seivan opened this issue 9 years ago • 5 comments

Instead of Equatable, how about Hashable based on its content? It would support being used in a Set and would use the same fields as the current implementation of Equatable supports.

seivan avatar Oct 10 '16 07:10 seivan

Hi, @seivan I see your point. But my concern is that not everybody needs Hashable for each type. And there are several ways how to implement hashValue If you think that there is a popular way, that we can generate, I'd love to see

sergdort avatar Oct 10 '16 21:10 sergdort

I'm thinking, that as long as every property is hashable it's class could be as well.

If you're hashable, you're also going to be equatable. So why not?

seivan avatar Oct 10 '16 22:10 seivan

I mean the popular way to generate hashValue is for example

struct Person: Hashable {
    var name: String
    var  age: Int
    var hashValue: Int {
          return name.hashValue ^ age.hashValue
    }
}

I wonder if its ok to generate it by default like this

If so, I would consider to make a separate xcode extension under this project which will generate Hashable conformance as well.

Because in my project for example we don't need Hashable for our domain objects

sergdort avatar Oct 21 '16 11:10 sergdort

I tend to use OrderedSet (custom) and Set a lot for when uniqueness matters, but that could just be me.

Thanks for ^, I didn't actually know about that!

seivan avatar Oct 21 '16 11:10 seivan

hi, @seivan, I have a implementation for this here, https://github.com/WANGjieJacques/CodeGenerator, this implementation is inspired by IntelliJ.

wangjiejacques avatar Jun 28 '17 15:06 wangjiejacques