-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Pattern-matching versus arrays, dictionary, list, dynamic? #10631
Comments
Is there an issue using array/collection initializer syntax for list patterns that would apply to anything int[] array = new [] { 1, 2, 3 };
var list = new List<int>() { 1, 2, 3 };
let { 1, var second, 2 } = array else return;
let { 1, 2, var third } = list else return; A sequence wildcard pattern would be nice to indicate that you don't care how many previous or subsequent elements there might be: var list = GetListOfNumbers();
let { var first, **, var last } = list else return; F# has no such facility. The cons pattern is interesting, but I'm not aware of a CLR type that it would map to cleanly. IIRC in functional languages it always is used to construct/deconstruct an immutable linked list. As for dynamic, I don't think that the current implementation provides a way to inspect truly dynamic types? Otherwise, I'd try to support the gamut of patterns through emitting code that inspects the dynamic type for the appropriate shape. dynamic expando = new ExpandoObject();
expando.Foo = 123;
expando.Bar = "456";
switch (expando) {
case { Foo is 123, Bar is "456" }:
Console.WriteLine("matched!");
break;
} |
As I have said in one of the other issues, I feel that arrays/lists will be well-served by a cons pattern coupled with list views/slices/segments. Enumerables are better off with LINQ. Dynamics can be matched against a property pattern. I have nothing to offer wrt dictionaries. |
I think complete analogy with object-or-collection-initializer would be really neat and I don't think it would be ambiguous at all (that's why we have a non-assignment-expression in collection initializers). And for dictionaries we can use indexer patterns (#10600 (comment)). |
If possible, please make that some sort of duck-typed
I propose using For dictionaries, how about:
To avoid exceptions when getting things that are simply not there, this would use, in order of availability:
The above pattern could also be adapted to random access list/collection index matching. |
Pattern matching with arrays & lists will be a bit difficult since C# (still) doesn't have array & list literals! Normally pattern matching is understandable because the syntax to deconstruct and to construct are the same. Consider tuples:
This has symmetry but looks really awkward... Any chance of C# finally having real, honest to god list and array literals? I'm going to completely rip off F# here with a suggestion:
And for arrays:
While the IEnumerable is not appropriate for deconstructing because, as hinted at above, you're quite possibly causing side effects or causing re-evaluation of some sequence by walking the IEnumerable. |
Actually, a better idea might be to support some duck typing here. The |
Issue moved to dotnet/csharplang #898 via ZenHub |
dynamic
interact with pattern matching?The text was updated successfully, but these errors were encountered: