Proposal: pattern matching of states #8744
Replies: 10 comments
-
I've wanted this for arbitrary types a couple of times. |
Beta Was this translation helpful? Give feedback.
-
Any news on this? Is it being considered? |
Beta Was this translation helpful? Give feedback.
-
This would be effectively matching against arbitrary values rather than constants, no? I don't see what it has to do with "state". |
Beta Was this translation helpful? Give feedback.
-
"An object is an entity that has state, behavior, and identity ..." |
Beta Was this translation helpful? Give feedback.
-
Thus far we've held to patterns being independent of enclosing state. It means that we can reason about exhaustiveness and reachability in switch statements and expressions, for example. I also think that it is a nice property for users to be able to trust: That a given pattern applied to the same value gives the same result. I do see the utility of comparing to a computed value, but I'd be hesitant to sacrifice the invariants around patterns to allow it as a shorthand. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Maybe it could be achieved without modifying the logic of and invariants of pattern matching (?) Since there would be no need to create a new variable in the case of same-type structs, maybe ... switch(myStruct)
{
case myOtherStructOfSameType:
...
break;
...
} ... could be translated into: if(myStruct.Equals(myOtherStructOfSameType)
{
}
else if (...)
{
...
} |
Beta Was this translation helpful? Give feedback.
-
This is important. I understand @MadsTorgersen point about exhaustiveness, but @svick's point about ns switch
{
var x when x.IsEmpty => 0,
var x when !x.IsEmpty => 0,
}; And the above code clearly is exhaustive. But I understand the compiler can't reason on the two expressions. As it could not reason on a different pattern. Enabling expressions instead of only types and constants, would allow me to do the following. Right now, I have to do this (2): int Sum(in ReadOnlySpan<int> ns) =>
ns switch
{
var x when x.IsEmpty => 0,
var (head, tail) => head + Sum(tail)
}; If expressions were allowed, we could do this (3): int Sum(in ReadOnlySpan<int> ns) =>
ns switch
{
ReadOnlySpan<int>.Empty => 0,
var (head, tail) => head + Sum(tail)
}; Which is much more readable. And right now, on snippet (2), if I remove the last pattern, I already get a warning. This shouldn't change if (3) was allowed. |
Beta Was this translation helpful? Give feedback.
-
@giggio I'm not sure that code would work the way you want, even if this feature was implemented, because ReadOnlySpan<int> ns = new int[0];
Console.WriteLine(ns == ReadOnlySpan<int>.Empty); |
Beta Was this translation helpful? Give feedback.
-
Any news about this proposal? Is it being considred? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
From C# 7 and on, you can write code like the following:
The idea is to allow a shortcut, like:
I must admit that I thought of this mainly for structs, but it could be extended to other types.
Beta Was this translation helpful? Give feedback.
All reactions