Replies: 4 comments
-
Would this kind of pattern matching allow deconstructing lists/arrays/IEnumerables by doing something like this?
If so, I think that when you're pattern matching
|
Beta Was this translation helpful? Give feedback.
-
@gafter Looks like this proposal has been left out from the feature triage? Is this something that could be considered for 9.0 timespan? |
Beta Was this translation helpful? Give feedback.
-
@alrz We do not have a specific proposal that is championed by any LDM member. |
Beta Was this translation helpful? Give feedback.
-
@gafter I had a few ideas around list patterns which I wrote up here as a proposal: https://gist.github.com/alrz/84addd150849a0b8c014deb85b75211d I'll be happy to contribute a prototype if anyone on the team is interested to champion it. Note that the fixed-length list patterns will be built entirely on the existing infrastructure. whereas variable-length list patterns would be built on top of span, ranges and relational patterns. |
Beta Was this translation helpful? Give feedback.
-
@gafter commented on Fri Apr 15 2016
dynamic
interact with pattern matching?@HaloFour commented on Sat Apr 16 2016
Is there an issue using array/collection initializer syntax for list patterns that would apply to anything
IList<T>
, including arrays?A sequence wildcard pattern would be nice to indicate that you don't care how many previous or subsequent elements there might be:
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.
IEnumerable<T>
is academically interesting but probably impractical.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.
@orthoxerox commented on Sun Apr 17 2016
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.
@alrz commented on Sun Apr 17 2016
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)).
@JesperTreetop commented on Sun Apr 17 2016
If possible, please make that some sort of duck-typed
IReadOnlyList<T>
instead -IEnumerable<T>
with an indexer with a getter ofT
and along
orint
Count
. (In the tradition of awaitables, foreach and collection initializers being speced using duck typing instead of interfaces. IfIReadOnlyList<T>
had been there right in 2.0, everyone would have adopted it and this wouldn't have been necessary.)I propose using
var first, ..., var last
instead, which conveys more "multi-elemental-ness" (it is literally the yada-yada-yada operator in Perl) than just repeating the*
. Or why notvar first, params, var last
? (Just kidding. 😜)For dictionaries, how about:
To avoid exceptions when getting things that are simply not there, this would use, in order of availability:
TryGetValue(TKey, out TValue)
(implementable without being a dictionary, again likeAdd
in collection initializers, would be picked up inIReadOnlyDictionary<TKey, TValue>
andDictionary<TKey, TValue>
)Contains(TKey)
followed bythis[TKey] => TValue
; not atomic and could throw during races, but not more or less dangerous than just writing that code outside of a pattern. Limited to dictionaries (like non-genericIDictionary
and Hashtable) that don't implementTryGetValue
.this[key]
, but throw if the indexer didThe above pattern could also be adapted to random access list/collection index matching.
@Richiban commented on Wed Aug 03 2016
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:
var (x, y) = (1, 2);
. There is a pleasing, easy-to-understand symmetry to this form, thanks to the tuple literal. How would it work with arrays?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
cons
idea seems right at first glance, I don't think it's right for C# since List is not a singly-linked list that can easily be pushed and popped like a stack.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.
@Richiban commented on Wed Aug 03 2016
Actually, a better idea might be to support some duck typing here.
The
case [var first, var second]
pattern can be used to deconstruct any type that supports an int-indexer, i.e. implementsthis[int index]
. Perhaps the type should also implementCount
sincecase [var first, var second]
technically will only match a list of length 2;Beta Was this translation helpful? Give feedback.
All reactions