Pattern matching on boolean fields: shorter syntax for matching 'true' #4363
Replies: 4 comments 14 replies
-
My first thought: what about matching false? Instinct says |
Beta Was this translation helpful? Give feedback.
-
I like the idea, but it is unfortunately going to be ambiguous with list patterns: #3435. Given the latest LDM decisions on the syntax for that, this code would be ambiguous: class C
{
public bool ConstOrProperty => true;
public int this[int index] => 1;
public int Count { get; }
}
const int ConstOrProperty = 1;
_ = (new C()) is { ConstOrProperty }; |
Beta Was this translation helpful? Give feedback.
-
Perhaps an implicit |
Beta Was this translation helpful? Give feedback.
-
Very interesting that I found this from just yesterday... have been wanting to post this proposal for some time as well. Today I got hit with another case where it would be much nicer to read if we had simplified pattern matching for boolean properties: return fileResponse switch
{
{ IsSuccessStatusCode: false, ReasonPhrase: var errorMessage, StatusCode: var statusCode } =>
new UnableToDownloadFile(errorMessage, statusCode),
... Would become: return fileResponse switch
{
{ not IsSuccessStatusCode, ReasonPhrase: var errorMessage, StatusCode: var statusCode } =>
new UnableToDownloadFile(errorMessage, statusCode),
... |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
In pattern matching you can currently have
I would like a shorter syntax for the
b: true
pattern match. If the field or property is a boolean, then you should be able to match it giving just the name, implying a test fortrue
. So,(The same pattern matching syntax should be available in switch statements as well as switch expressions.)
Often boolean fields or properties have a name which describes the condition. The shorter syntax would be particularly useful when matching several of these flags:
(I envisage that as a formal language proposal, the two tokens
: true
would be implicitly added if the pattern match consists of just a single field or property name and nothing more. That would then give a type error if you tried to specify this kind of match for a non-boolean field.)Beta Was this translation helpful? Give feedback.
All reactions