Proposal: list-patterns on enumerables
Allow is [ 1, 2, 3 ] (list pattern) and is [ 1, ..var x, 5 ] (slice pattern) on enumerables.
Speclet: https://github.com/dotnet/csharplang/blob/main/proposals/list-patterns-enumerables.md
Background:
- https://github.com/dotnet/csharplang/blob/main/proposals/csharp-11.0/list-patterns.md
- https://github.com/dotnet/csharplang/blob/main/proposals/csharp-8.0/ranges.md
Design meetings:
- https://github.com/dotnet/csharplang/blob/main/meetings/2022/LDM-2022-10-19.md
- https://github.com/dotnet/csharplang/blob/main/meetings/2023/LDM-2023-10-09.md#list-patterns-on-enumerables
why not [1; x] [1; x; 5]
since it's a new language proposal, we can throw away that var, similar to how we can write lambda without type name on its parameter, it just matches
maybe we can even do var myLambda = [1; x] => true | unmatched => false; myLambda(myList);
It's not a new language feature. It already exists for indexable collections, like arrays and lists.
[1; x] feels like a (personally) long awaited pattern matching on non const values rather that x being a declared variable here. Also, as stated above it's already a feature on indexable types. It's just that it's harder to do properly with sequences because it most likely will result in implicit buffering in complex cases like [1, ..var x, 5] which might be not desirable. At least with indexable collections this behavior is explicit and based on public members of those collections.