dotnet / csharplang

The official repo for the design of the C# programming language
11.61k stars 1.03k forks source link

Proposal: list-patterns on enumerables #6574

Open jcouv opened 2 years ago

jcouv commented 2 years ago

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:

Design meetings:

Xyncgas commented 1 year ago

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

KennethHoff commented 1 year ago

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

En3Tho commented 1 year ago

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