List pattern indexer resolution problematic #8540
-
(My apologies if this already has its own topic elsewhere. Github's search function making it hard to search for phrases and not just words made it difficult for me to check if there already exist such a topic, given that words like "list" and "pattern" occur in countless topics here...) For the list pattern, the rules the compiler follows to pick an indexer are currently as follows (https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-11.0/list-patterns):
If a type features both an This is problematic because this rule is reliant on the bold assumption that the I therefore propose to change the rules to:
and adopt the compiler behavior accordingly. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Those apis should be updated to support Index. |
Beta Was this translation helpful? Give feedback.
-
This honestly just seems like a compiler bug. I don't think the spec needs to change. |
Beta Was this translation helpful? Give feedback.
-
As it turns out (discussed in a different topic), the list pattern follows the already established precedent where for example using an index like |
Beta Was this translation helpful? Give feedback.
As it turns out (discussed in a different topic), the list pattern follows the already established precedent where for example using an index like
[^1]
would also call theobject
-based indexer but not theint
-based indexer. I have no broader insight into the wider .NET ecosystem and developer community, and what it would effect if features like these were tidied up to make their behavior seem less surprising and arbitrary, so i leave it to the team what to do about it. Or likely not to do, considering thatIndex
andRange
are around already since the Core 3.0 days (and so is quite likely the behavior of favoringobject
-based indexers for them). While i have to admit that on a personal lev…