csharplang icon indicating copy to clipboard operation
csharplang copied to clipboard

Proposal: list-patterns on enumerables

Open jcouv opened this issue 3 years ago • 3 comments

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

jcouv avatar Oct 19 '22 20:10 jcouv

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);

Xyncgas avatar Jun 12 '23 01:06 Xyncgas

It's not a new language feature. It already exists for indexable collections, like arrays and lists.

KennethHoff avatar Jul 22 '23 18:07 KennethHoff

[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.

En3Tho avatar Jul 22 '23 19:07 En3Tho