-
Notifications
You must be signed in to change notification settings - Fork 1k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
Proposal: Special equality operator when one argument is null #2340
Comments
Have you considered using |
@yaakov-h This isn't for my code; I can always jump through whatever hoops are necessary within my own library. It's for .NET consumers at large, most of whom are used to writing |
The Writing I'd therefore suggest that you keep this operator: public static bool operator ==(Utf8String left, Utf8String right); and ditch this one: public static bool operator ==(Utf8String left, string right); Instead, implement |
@theunrepentantgeek Except one case. If |
@theunrepentantgeek There was a symmetric |
Overload resolution could prefer the matching type to resolve the ambiguity? e.g. // preferred for Utf8String == null or null == Utf8String
public static bool operator ==(Utf8String left, Utf8String right);
public static bool operator ==(Utf8String left, string right);
// preferred for string == null or null == string
public static bool operator ==(string left, string right);
public static bool operator ==(string left, Utf8String right); |
And if there is no overload for |
@yaakov-h not sure I understand; the ambiguity exists between the matching types and the alternative type. If there was only one there wouldn't be any ambiguity |
If you had 2 equality operators of different types; then yes the ambiguity remains public static bool operator ==(Utf8String left, string right);
public static bool operator ==(Utf8String left, ThisType right);
// Utf8String == null ambiguous But then you could add a matching type and it would win the tie break public static bool operator ==(Utf8String left, string right);
public static bool operator ==(Utf8String left, ThisType right);
public static bool operator ==(Utf8String left, Utf8String right);
// Utf8String == null; matches Utf8String == Utf8String |
@benaadams's suggestion to prefer the @yaakov-h I highly doubt that there are many types which overload |
@PathogenDavid possibly not, but I do love edge cases. |
Also consider the Null obejct pattern and have a Utf8String.Null which can be leveraged although that won't stop the ambiguity which is caused by having two overloads and preventing generic null from being used without a cast. You could drop the 2nd overload as that is what causes it in general but that won't get you to the overload you need... Instead perhaps an implicit cast to string could be provided but I can't see how that wouldn't need to alloc. Perhaps an implicit cast to ReadOnlySpan byte would be more suited and then combining that with the null object pattern works but I've jumped through hoops. Tldr; I really wish I could special case null in my operators explicitly rather than checking for them as it would lend itself to defaults I could define as well as solve issues like this.. public static explicit operator null(Type type) But I do this today with object.ReferenceEquals Even without the Type overloads just the null explicitly would be useful there imho with type support being an enhancement later. Would also be nice to have a way to override is and default as well but that's another story? public static explicit operator default T(T t){ } public static explicit operator is boolean(T t){ } You get around the generics by using the Type syntax above but I think it needs refinement for sure since you would want to control what your returning and when to do different things in different scenarios.... but then again... leveraging contravariance there could also be useful... |
Another possibly simpler way to solve the problem originally described here would be to introduce an attribute |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Problem
In the
Utf8String
prototype I've defined twooperator ==
methods:However, now simple equality / inequality comparisons against null will not properly compile, as the statement
if (myUtf8String == null)
is an ambiguous call between the two methods, since null matches both signatures.It would be nice if I could define an
operator ==
overload which would explicitly match null, so the compiler would no longer see an ambiguity given the code snippet above.There's an additional benefit to this proposal. Say that I didn't have the string-based overload of
operator ==
and there was no ambiguity to begin with. Even if one side of the operator were a literal null, the compiler still emits a call to the normaloperator ==
method, and it would still go through unoptimized code paths before determining that it can early exit. By having a null-specificoperator ==
overload, the type author can write the simplest possible implementation of the method. It'll give behavior similar to the special support the C# compiler has today forif (myString == null)
, but without requiring the compiler to special-case particular types.The text was updated successfully, but these errors were encountered: