-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Open issue: non-negative assumption should apply to both Count and Length #5137
Comments
Something I've always been curious about, why are Also, why have two interfaces that are essentially the same when you could just have a single Should |
Unsigned integers were not cls compliant. |
When the compiler encounters this line will it already be sure that it's working with a collection or sequence? _ = t is { Length: -1 }; // error CS8518: An expression of type 'T' can never match the provided pattern. If the compiler can't be completely sure that |
Our position is that it's fine for the language to assume these types are well behaved for the purposes of pattern matching. |
Why not put an attribute on the correct length property (like class C {
public object this[int i] { get; }
[IndexableLength(allowNegative = false)]
public int Size { get; }
}
_ = new C() is { Size: -1 }; // error |
Because the APIs have already shipped, and we'd want patterns to work across the enormous ecosystem of well-behaving types already out there. |
Yes, it will only make this assumption when the type is both Countable and Indexable, where those words come from the index and range feature in C# 8. |
I suppose LongLength would be out of scope here because it doesn't have any special meaning. |
FYI, I added a third question to OP for Wednesday: |
This was discussed in LDM 10/20/2021. In short:
|
Does this mean:
|
The former "If both are present, only one of them is assumed to be non-negative". If there is a Length, then we say/assume nothing about Count. |
Should the non-negative assumption apply to the used Property or both?
The current behavior is shown below. I think we should have an error for both lines.
Should we assume that Length and Count have the same value?
Another related question is whether we should assume
Length
andCount
to have the same value? For example:case { Length: > 0 }: case { Count: 0 }: case { Count: var unreachable }:
.Should we assume non-null slice when we expect a non-zero slice?
Should we consider
null or { Length: not 4 } => 0, [_, .. [_, _], _] => 0
exhaustive in a nullable context?It is possible for a type to claim
Length=4
but return a null slice, but that would violate our assumption that lengths and elements are consistent between outer and nested lists.Options:
FYI @alrz @333fred
Relates to proposal #3435
Relates to test plan dotnet/roslyn#51289
The text was updated successfully, but these errors were encountered: