Proposal: Auto implemented operators for equality and comparison #2160
-
MotivationWhen implementing public struct Number : IEquatable<Number>, IComparable<Number>
{
private long Value {get; }
public Number(long value) => Value = value;
public bool Equals(Object other) => other is Number && Equals((Number)other);
public bool Equals(Number other) => other.Value == Value;
public static bool operator ==(Number n1, Number n2) => n1.Equals(n2);
public static bool operator !=(Number n1, Number n2) => !(n1 == n2);
public int CompareTo(Number other) => Value.CompareTo(other.Value);
public static boolean operator >(Number n1, Number n2) => n1.CompareTo(n2) > 0;
public static boolean operator <(Number n1, Number n2) => n1.CompareTo(n2) < 0;
public static boolean operator >=(Number n1, Number n2) => n1.CompareTo(n2) >= 0;
public static boolean operator <=(Number n1, Number n2) => n1.CompareTo(n2) <= 0;
} Why the compiler don't just generate all the non-implemented operators for me when implementing So this: public struct Number : IEquatable<Number>, IComparable<Number>
{
private long Value {get; }
public Number(long value) => Value = value;
public bool Equals(Number other) => other.Value == Value;
public int CompareTo(Number other) => Value.CompareTo(other.Value);
} Its trasladated to this: public struct Number : IEquatable<Number>, IComparable<Number>
{
private long Value {get; }
public Number(long value) => Value = value;
public bool Equals(Number other) => other.Value == Value;
public int CompareTo(Number other) => Value.CompareTo(other.Value);
//Compiler Generated (Equality)
public bool Equals(Object other) => other is Number && Equals((Number)other);
public static bool operator ==(Number n1, Number n2) => n1.Equals(n2);
public static bool operator !=(Number n1, Number n2) => !(n1 == n2);
//Compiler Generated (Comparison)
public static boolean operator >(Number n1, Number n2) => n1.CompareTo(n2) > 0;
public static boolean operator <(Number n1, Number n2) => n1.CompareTo(n2) < 0;
public static boolean operator >=(Number n1, Number n2) => n1.CompareTo(n2) >= 0;
public static boolean operator <=(Number n1, Number n2) => n1.CompareTo(n2) <= 0;
} When the number of methods in an app should be considered an attribute for example Issues
Related |
Beta Was this translation helpful? Give feedback.
Replies: 11 comments
-
This would change the behavior if existing code and introduce breaking changes. Any such feature should definitely be opt-in. |
Beta Was this translation helpful? Give feedback.
-
Where could break existing code? Compiler should only generate operators if aren't already implemented. |
Beta Was this translation helpful? Give feedback.
-
When types don't define an equality operator you can still use |
Beta Was this translation helpful? Give feedback.
-
I wish though that it had never been allowed to override both == and !=, Both > and <=, and both < and >= |
Beta Was this translation helpful? Give feedback.
-
Note that your suggested implementations aren't null-safe (which is all the more reason to somehow get the compiler to generate them -- lots of people don't implement them properly). |
Beta Was this translation helpful? Give feedback.
-
💭 I wonder if it would be worthwhile considering the Spaceship operator |
Beta Was this translation helpful? Give feedback.
-
Proposing tank operator: o[=^=]o |
Beta Was this translation helpful? Give feedback.
-
As a "compromise" could Roslyn generate them when the light bulb is pressed? If I remember correctly it already does but the generated methods simply throw NotImplementedException. |
Beta Was this translation helpful? Give feedback.
-
@fanoI PRs welcome. |
Beta Was this translation helpful? Give feedback.
-
We can add some [AutoGenerateOperators] attribute to the CompareTo method, so this new behavior is totally intended and controlled and breaks nothing. |
Beta Was this translation helpful? Give feedback.
-
That will certainly be a reality with Source Generators. |
Beta Was this translation helpful? Give feedback.
@VBAndCs
That will certainly be a reality with Source Generators.