Skip to content
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

Convenient ways to define == and hash #53565

Open
aplavin opened this issue Mar 2, 2024 · 2 comments
Open

Convenient ways to define == and hash #53565

aplavin opened this issue Mar 2, 2024 · 2 comments
Labels
design Design of APIs or of the language itself equality Issues relating to equality relations: ==, ===, isequal feature Indicates new feature / enhancement requests hashing

Comments

@aplavin
Copy link
Contributor

aplavin commented Mar 2, 2024

There are more and more packages that basically help users define ==/isequal/hash for their structs in a straightforward way – by forwarding to the struct fields.

A (potentially incomplete) list:
https://github.com/JuliaServices/AutoHashEquals.jl
https://github.com/jolin-io/StructEquality.jl
https://github.com/jw3126/StructHelpers.jl
JuliaRegistries/General#102035
Each is subtly different, all are in use! (aside from the last one, being registered)

I think this proliferation is a clear sign that Julia itself should provide "the definite" solution for this core problem.
A years-old discussion at #4648 focused on changing the default behavior, but that ship has sailed. So, there should be some simple opt-in for users.

I'm not too familiar with differences of those packages, only used https://github.com/jw3126/StructHelpers.jl a few times. Maybe, someone more knowledgeable can suggest a design that Base can use?

Pinging those who recently committed to those packages: @ericphanson @gafter @mcmcgrath13 @jw3126 @matthias314

@BioTurboNick
Copy link
Contributor

From the Slack thread, it's notable that other languages like Python (@dataclass: https://docs.python.org/3/library/dataclasses.html) and C# (record: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/record), among others, have seen fit to bring this feature into the language in some official capacity.

Suggests that some standard solution would be a benefit, even if it is not a priority.

@matthias314
Copy link
Contributor

I think it would be great if this could be added to Julia. The first question would be if the maintainers are open to such an idea. The design would depend on the functionality one wants to implement. A very modest approach would be a macro defining ==, isequal and hash for a type T based on a list of fields that defaults to fieldnames(T). That wouldn't need much code.

BTW, my package https://github.com/matthias314/StructEqualHash.jl is now in the registry.

@nsajko nsajko added design Design of APIs or of the language itself hashing feature Indicates new feature / enhancement requests equality Issues relating to equality relations: ==, ===, isequal labels Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design Design of APIs or of the language itself equality Issues relating to equality relations: ==, ===, isequal feature Indicates new feature / enhancement requests hashing
Projects
None yet
Development

No branches or pull requests

4 participants