-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
API proposal: MemoryExtensions.ReferenceEqual #72968
Comments
FWIW, that was what I expected at first glance. Considering that and the potential confusion with |
Maybe |
|
I had considered "are same" but didn't like it because "same" and "equal" are too similar. The ambiguity would still exist. Another alternative is |
namespace System;
public partial static class MemoryExtensions
{
public static bool ReferenceEqual<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> other);
public static bool ReferenceEqual<T>(this Span<T> span, ReadOnlySpan<T> other);
} |
|
Moving to 8.0.0 since we don't have agreement on the name yet. |
API proposal:
MemoryExtensions.ReferenceEqual
See #54794 for context. In a nutshell, we want developers to stop using the equality and inequality operators on
Span<T>
andReadOnlySpan<T>
.But there are some cases where callers really do need to check for referential equality, such as if they're trying to special-case "an arbitrary empty span" vs. "a span that specifically references nullptr." Another use case is string interning dictionaries, where callers may want to know as an optimization whether a span references a specific instance of a string value rather than simply containing the same contents.
Proposed API
Discussion
If we eventually obsolete the span equality comparison operators, callers will need to answer the question "was this comparison intended to be a referential equality comparison or a value equality comparison?" We're betting on that most callers wanted value equality and will opt to change their call sites to target the existing
SequenceEqual
method. The newReferenceEqual
method proposed here provides an alternative for developers to preserve reference-equality comparison without needing to#pragma warning disable ...
every call to the existing equality operators..NET already exposes two related contepts:
static bool object.ReferenceEquals(object, object)
, which is pure referential equality; andstatic bool MemoryExtensions.SequenceEqual<T>(Span<T>, Span<T>) where T : IEquatable<T>
, which performs content equality comparison.For consistency with the existing
MemoryExtensions.SequenceEqual
API, we'll keep its parameter names in the proposedMemoryExtensions.ReferenceEqual
API. We'll also drop the s fromReferenceEquals
to match the existing extension method naming conventions.Risks
The name
ReferenceEqual
might inadvertently convey "only referential equality" instead of the intended "both referential and length equality" behavior. That is, somebody might expect the following to return true.We may be able to mitigate this risk through careful messaging and encouraging callers to think about "references to blocks of memory" rather than "references to addresses in memory". If you consider that a block of memory contains both a start and an end position - as distinct from just a single address - then the analogy makes sense.
The text was updated successfully, but these errors were encountered: