Replies: 3 comments 7 replies
-
Closing as this is a request for a language change vs. being a compiler bug. Please open this as a discussion on dotnet/csharplang. |
Beta Was this translation helpful? Give feedback.
-
Duplicate of #6574 |
Beta Was this translation helpful? Give feedback.
-
I see that proposal #9005 would cover this request. However, the request here is quite doable and improves the outcome for code that follows published guidelines. Indeed I was surprised this case did not work OOTB. I often have code where the caller knows whether an empty collection just returned from somewhere is an error or needs to be handled differently. Having seen many careless "helpful helpers" to address this, I'd prefer a proper language idiom. Would you take patches? |
Beta Was this translation helpful? Give feedback.
-
It is possible to pattern-match on an empty list/array/string/span/whatnot:
It is clear why this should remain unworkable for enumerables. But there is no technical reason this can't work for collections (ICollection, IReadOnlyCollection), which are frequently returned from calls. There is even guidance to do so.
Currently, I get
which forces me to fall back to
x is null or {Count: 0}
(or Length). Not too bad, but it harms regularity.Thus, I suggest lifting this restriction in the particular case of an emptiness pattern.
https://sharplab.io/#v2:CYLg1APgAgTAjAWAFBQAwAIpwCwG5nJQDMmM6AwugN7Lp2YlTboCyAFAJTW328D0fdAEl0AUwAeAB1EBjAC7o5Ae3QA7JQoDuSgE4BrED150BmOEQA8AS1VyAfOnHoAvOgDaAXXxJj9I8dMRCWl5RRVtfUMfX3RA8iUAGwTZOSslVWtbBydXT28Y2MEhACVRAENgAHlVBIBPeKSUtIybe0cXdy9/Xm76UwB1XT0AZyiC1s923K7o30DSiuq6gBkrYblMtpzO/JjA1fXN7I683pNBLAxtgCJr3d8rADN0Nic1tQBXJPRdTq45AAWOiUmjUolBAEEdABzD4AW1EtgAouIZKJJKl0px7ugAL7IXFAA=
Beta Was this translation helpful? Give feedback.
All reactions