Generator.Equals icon indicating copy to clipboard operation
Generator.Equals copied to clipboard

Support for struct

Open Socolin opened this issue 3 years ago • 3 comments

Hello,

Would it be possible to also generate equality members for struct ?

Socolin avatar Aug 26 '22 18:08 Socolin

Yes, although I can't think of a scenario where I would ever use it. Out of curiosity, how would you use it?

diegofrata avatar Aug 26 '22 18:08 diegofrata

I have some enums like this one and I would like to generate the equality members automatically

[Equatable]
public readonly struct Color
{
	[DefaultEquality]
	public int R { get; }
	[DefaultEquality]
	public int G { get; }
	[DefaultEquality]
	public int B { get; }
	[DefaultEquality]
	public int A { get; }
}

Socolin avatar Aug 26 '22 18:08 Socolin

That should already be equatable by default -- as long as every field in a struct is a value type, equality should work. The only caveat is that, apparently, it uses reflection which is slower. I think it's easy enough to add support to structs, I will have a look.

By the way, you only need to specify DefaultEquality if using explicit mode. This would be shorter:

[Equatable]
public readonly struct Color
{
	public int R { get; }
	public int G { get; }
	public int B { get; }
	public int A { get; }
}

diegofrata avatar Aug 26 '22 19:08 diegofrata

This is now available in version 2.6.0

diegofrata avatar Oct 24 '22 13:10 diegofrata