-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Proposal: List, Indexer (Dictionary) and String Patterns #5811
Comments
I have a bit of a problem with a general expression appearing as part of the pattern, as you have in the indexer pattern. |
@gafter to avoid assignments in the pattern I think conditional-or-expression would be sufficient, right? |
@alrz To avoid side-effects I suggest constant-expression. |
What if I want to capture that void Value Cond(IReadOnlyList<Expr> list)
{
while (list.Count != 0) {
switch (list) {
case { defaultCase }:
return defaultCase.Evaluate();
case { condition, result, *** tail }:
if (condition.Evaluate() != Value.Nil) {
return result.Evaluate();
} else {
list = tail;
}
break;
}
}
return Value.Nil;
} |
@orthoxerox How exactly would that work? How would the compiler create an |
How do you figure out whether the count matches before iterating if the type is just Or is that just an optimization that does not apply to general |
@orthoxerox Wildcards are used solely for ignoring items. Actually, you're asking for array slices (#120) in patterns, which are not necessary. you can still use the slicing syntax in the |
@svick It will match the length while iterating, so if |
Isn't syntax going to clash with collection initialisers, and array literals (VB). If thisString Like "[A-Z][A-Z]#" Then |
Structure Range
Public ReadOnly Property XB As Int32 'Starting Index
Public ReadOnly Property XE As Int32 'Finishing Index
End Structure Which would allow use the specify a range as an Indexer parameter |
@AdamSpeight2008 The syntax proposed here is not intended to be used in VB. The pattern that |
@alrz
|
|
@alrz I've got mixed feelings about the string pattern part. It is a very powerful tool in the form as you propose, but of course they have a problem of silently introducing regular expression machinery into a simple syntactic construction. There was enough outcry against the fact that innocently-looking My preference would be to (somehow) bring the "back interpolation" to the |
@vladd not silently, you have to use regular expression literal syntax: |
Recent proposals on wildcards (#14794, #14862) use the underscore character Double-underscore I support something like @AdamSpeight2008's proposed syntax for sequence wildcards. Though I prefer a single (not double) underscore character followed by the repetition character. And I added tuple is (1, _*) // tuple is oneple (1), or (1, _), or (1, _, _), or (1, _, _, _), etc.
tuple is (1, _+) // tuple is (1, _, _*)
tuple is (1, _?) // tuple is oneple (1) or (1, _) only This syntax could also apply to sequence wildcards for use in lambda argument lists (#15027): Loaded += _* => { /* not using the arguments */ };
Func<bool> f1 = _* => true;
Func<int, bool> f2 = _* => true;
Func<int, int, bool> f3 = _* => true; |
Out of date. |
Following patterns are based on the syntax proposed in #206.
List Patterns
List patterns can be used on arrays and all types that have implemented the
IList
interface . It would iterate through the list and match the list-subpatterns.Syntax
Remarks
{ }
matches an empty list. Determined byEnumerable.Any
orICollection.Count
based on type.**
matches any sequence of one or more items.***
matches any sequence of zero or more items.EDIT 1: Sequence wildcards are capable to be used also in tuple patterns.
It would match a tuple with any length equal or greater than two.
EDIT 2: It would be nice to be able to use slices (#120) to catch the "rest" of the list at once, e.g.
Examples
Indexer Patterns
Indexer patterns can be used on all types with an indexer, just like dictionary initializers.
Syntax
An indexer pattern matches the values extracted by the use of indexer with the specified arguments. Following the property pattern syntax, we use an
is
keyword instead of=
sign. The whole match fails only if the one of indexer subpatterns fail. The order in which subpatterns are matched is not specified, and a failed match may not match all subpatterns. It's a compiler-error if type of the expression _e_ have not defined an indexer with parameters of type constant-expression-list, hence it's pattern incompatible.EDIT: The indexer pattern syntax can be mixed with property-pattern so one can check a indexer and a property in the same pattern (analogous to object initializers).
String Patterns
String patterns can be considered as inverted string interpolation, and of course they can be combined with regular expression literals (#5806):
Behind the scene, this creates a regular expression group at the interpolating section (
{ }
) and applies the formatting part (\d+
) to it, when the regex succeed, then it tries to convert the group capture viaTypeConverter
back to the variable type. If regular expression doesn't match or conversion wasn't successful, pattern fails.Default type for variables in string patterns is
System.String
. The syntax for string patterns is pretty much the same as string interpolation, but formatting part after colon has a different meaning. This can be extended to bind repeating captures to an array orIEnumerable
.The text was updated successfully, but these errors were encountered: