-
Notifications
You must be signed in to change notification settings - Fork 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: default expression as a pattern #766
Comments
I think |
@alrz interesting, thank you for linking your issue. From reading it I realized that it works fine for both reference types and for non-nullable value types. It also works for reference-type-constrained type parameters, but fails for value-type-constrained type parameters. |
AFAIK it should work for all cases because it's a constant. You could report it on roslyn repo as well. |
I think |
Just checked with VS2017 Update 3, Preview 10 and you are correct: In addition, |
It works except in the context of generics without a reference constraint. bool F<T>(T x) => x is default; // CS0150 A constant value is expected the following works bool F<T>(T x) where T: class => x is default; |
Ah, my apologies. I didn't check it with generics. You are right, we aren't quite there yet with this one. |
you may want to close this; |
@alrz thank you for cross-referencing. |
While I have discussed a fair number of proposals, I have never written one myself, so take it easy on me if this is crazy 😆.
Proposal
A very minor, perhaps trivial annoyance with pattern matching currently is the restriction on what values can be used as patterns.
Proposal: allow the use of
default
as a constant-like pattern against which a value may be matched.Currently proposals/patterns.md states the following
I propose the following change
pattern : type_pattern | constant_pattern | discard_pattern | var_pattern | recursive_pattern + | default_pattern ;
Default Pattern
A default pattern tests the value of an expression against the default value of some type. The default must be the token
default
ordefault(T)
for someT
. If no type is specified,default
represents the default value of the type of the expression. The pattern default is considered matching the expression e ifobject.Equals(default, e)
would returntrue
.Motivation
With the introduction of Generic patterns in C# 7.1, pattern matching has become much more broadly applicable and usable. This opens up powerful new scenarios and thereby exposes some minor limitations when writing generic code that uses patterns.
C# 7.1 also introduced the wonderful
Target-typed "default" literal feature.
With these two features now in the language, the introduction of the proposed form would both increase (intuitive) consistency and provide a pleasant, although admittedly minor, syntactic sugar.
The Argument from (intuitive) Consistency
The following code is legal:
default
in the above is used in a place where a compile time constant was required in previous versions of the language.Example Use Case
My admittedly unsound
IEqualityComparer<T>
which provides for fuzziness, has the followingGetHashCode
implementation:But with a Default Pattern I could write
Not much shorter, but it reads well.
Remarks
This may not be worth the effort given how minimal the gain is, but I wanted to throw it out there, the idea only occurred to me 20 minutes ago when I was refactoring something, so I wanted to post this before my feet either became frozen or my brain paralyzed with analysis.
The text was updated successfully, but these errors were encountered: