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

Map struct to record classes (and structs), use default Equals/GetHas… #2235

Merged
merged 2 commits into from
May 29, 2024

Conversation

bernardnormier
Copy link
Member

@bernardnormier bernardnormier commented May 29, 2024

…hCode

This PR changes the struct-to-C# class mapping: the mapped class is not a (read-write) record class with its default Equals and GetHashCode implementation.

Fixes #2214.

{
baseNames.push_back("System.IEquatable<" + name + ">");
}
_out << nl << "public " << (classMapping ? "sealed partial record class" : "partial record struct") << ' ' << name;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is common for record classes and structs to be read-only. However, here they are read-write for backwards source compatibility.

@@ -2235,40 +2229,6 @@ Slice::Gen::TypesVisitor::visitStructEnd(const StructPtr& p)
_out << nl << "ice_initialize();";
_out << eb;

if (classMapping)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We get all this (and more) for free from the record class.

@@ -35,7 +19,7 @@ public static void HashCodeAdd<TSource>(ref HashCode hash, IEnumerable<TSource>
}
}

public static bool DictionaryEquals(IDictionary d1, IDictionary d2)
internal static bool DictionaryEquals(IDictionary d1, IDictionary d2)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is still bogus and used internally. Will cleanup in a follow-up PR.

@@ -28,40 +28,38 @@ private static void allTests(Ice.Communicator communicator)
{
S2 v;

v = def_s2.Clone();
test(v.Equals(def_s2));
v = def_s2 with { };
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept mostly the same logic in this test. Just cleaned it up.

@bernardnormier bernardnormier merged commit e2e40e3 into zeroc-ice:main May 29, 2024
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generated Equals for struct mapped to class in C#
3 participants