-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Hashable for Struct #9166
Add Hashable for Struct #9166
Conversation
@maoyama thanks for the PR. What about adding an option (e.g. |
@wing328 I didn't want to add more options and make it more complicated for the user. I like the simple specs by this PR. |
Understood. Let's see if the Swift technical committee has a different option on this as I do not know if everyone likes hashable struct. |
Another use case worth considering is that a user wants all models except a few models to extends hashable. An option I described above can achieve that. |
I don't see any downside in making all the models conform to |
OK. Let's go with this and add an option if needed later. |
Hey guys @4brunu . I've experienced a downside of this feature. I have many custom implementations of Hashable that seem to be ignored now, since the model has a synthesized implementation. So for me it will be crucial to be able to control this feature. For me it would be best to be able to control this at model-level. Like with We could also have a configuration flag to set the default behavior. |
A configuration flag to control the behaviour seems a good option to me. |
I want to know more about this custom implementation of Hashable that seem to be ignored. // Custom implementations of Hashable
extension Animal {
static public func == (lhs: Animal, rhs: Animal) -> Bool {
return lhs.className == rhs.className
}
public func hash(into hasher: inout Hasher) {
hasher.combine(className)
}
}
// Generated Model
public struct Animal: Codable, Hashable {
public var className: String
public var color: String? = "red"
public init(className: String, color: String? = "red") {
self.className = className
self.color = color
}
} (If this feature has a downside, we need an option to control its behavior.) |
@maoyama I suppose when the extension is in the same file, the custom implementation is used. But this is not applicable for generated code files, of course. In my case I even get a compiler warning, that my implementations will be ignored. |
@fl034 Thank you for your reply. Here are the results of the custom implementation I've tried:
I can open a PR with adding options. |
@maoyama a PR is welcome 👍 |
Resolves #9030
PR checklist
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*
.For Windows users, please run the script in Git BASH.
master
,5.1.x
,6.0.x
@jgavris (2017/07) @ehyche (2017/08) @Edubits (2017/09) @jaz-ah (2017/09) @4brunu (2019/11)