Pattern matching syntax for non-constant values #8891
Replies: 2 comments 2 replies
-
Beta Was this translation helpful? Give feedback.
-
Is this really a fit for pattern matching, though? I think that implies that the thing being matched is a constant/literal of some kind. Arguably something that changes, like a variable, might not really create a "pattern". For example, what happens if I do like this idea, but it seems like it might make more sense to use a "trinary" operator that is syntactic sugar for the binary operator form and would keep the direction of the operators the same/consistent. So something like: if (_array.Length >= min && _array.Length < max)
{
// ...
}
// becomes:
if (min <= _array.Length < max)
{
// ...
} You can still cause the error with |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently, pattern matching is a neat and extremely easy way of expressing conditions for a variable against constants. For example:
However, the pattern matching syntax is not valid when non-constant values are used in place of the constants. For example, this will not work:
Instead, it will look like:
Code using non-constant values with pattern matching expressions will:
Along with all other reasons the existing pattern matching syntax was introduced, the three listed reasons will correlate with things like reducing developer errors in logic and improving maintainability. The syntax would be a quality factor for developers.
Beta Was this translation helpful? Give feedback.
All reactions