-
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
Disallow is var x
in c#8
#1203
Comments
The |
@HaloFour I think OP meant only the top level pattern in |
The
I read somewhere that people use Now, when it has some known uses (for whatever reason) you can no longer suddenly make it an error. That's actually a factor to not take a breaking change. We could just suggest to use declaration expressions instead via an IDE code action, whenever we have it. |
If this were pre-C# 7.0 I'd probably agree with this proposal. I want to say that it's come up before. But at this point it's a part of the language, and one that people do seem to [ab]use for inline variable declaration. To remove it from the language is certainly a breaking change and quite impractical. With C# 8.0 non-nullable reference types there will at least be the additional null checking in flow analysis which should detect when a potential |
F# give warnings when a type test always hold (e.g. let f (c : C) =
match c with :? C -> () // WARNING But not if you just use a variable (e.g. let f (c : C) =
match c with x -> () // OK I think there is certainly some space to produce useful warnings here (as part of warning waves dotnet/roslyn#1580), but whether or not Relates to dotnet/roslyn#16099 |
This change would break my code for no reason other than a few folk don't like the pattern. That would, in my view, be a ridiculous thing to do. The correct thing to do would be to introduce declaration expressions, removing the need to use |
Another thing to keep in mind is that if (_dependency.MutableValue is var localCopy && localCopy != null)
// instead of:
//var localCopy = _dependency.MutableValue;
//if (localCopy != null) This might not seem like much now, but I'd consider it preparation for when property patterns land, at which point you can change it to |
If you want to prohibit a language feature.......................................... write an analyzer. 😄 |
But this is currently valid, useful and already works: public enum Color { Blue, Green, Red }
public Color ParseColorFromCommandLine()
{
switch(Console.ReadLine())
{
case "Red": return Color.Red;
case "Blue": return Color.Blue;
case "Green": return Color.Green;
case var other: throw new InvalidOperationException($"{other} is not a valid color");
}
} In the public void M(string s)
{
if(s is var x) // Warning: expression is always true
{
...
}
else
{
...
}
} |
That could've been a useful suggestion when the first steps to pattern matching was still being worked on, but introducing new warnings for an existing feature is a breaking change for people who compile with Until then, you can make do with a custom analyzer. |
I could be wrong, but I thought that compiling with |
Discussion is not a problem. If you find it inconclusive, let me make it clear right now: we will not introduce a breaking change here. That should make this discussion quite conclusive. Problem solved.
Eric is correct here: Closing this issue, as the discussion is now conclusive and the language design team has no intention of making a breaking change. However, you are welcome to continue discussion here if you want. |
I thought the same, but on the else statement (CS0162, Unreachable code detected); it doesn't at the moement, and not on typed matches that can be proven true either. |
There has been a lot of confusion and inconclusive discussion about behavior of this syntax. I think c# team should consider to eliminate this problem in appropriate time.
behavior of this syntax is inconsistent despite the behavior of classic
x is Foo
which returns false in case x is null even if variable is of provided type.Also
is var x
is becoming a bad practice to force inline every variable declaration. We should avoid this.We must not encourage use of it by introducing alternative syntax just to exclude null from its domain.
I tried to find a proverb from internet to describe current situation 😄 :
《The engine is falling to pieces while the joint owners of the car argue whether the footbrake or the handbrake should be applied》
Meanwhile we must not change behavior of this pattern because it would be very subtle and hard to debug for every developer who dumped some of this in his code base.
I guess the only solution is to disallow this syntax entirely. C#8 is good time line and as of c#7.3 there should be warning using it.
If you dont put value to my speaks, i think Eric lippert is a well trusted person here so read Q&A then tell me how his speaks renders this syntax inappropriate.
https://stackoverflow.com/a/7640437/4767498
Thanks for reading. Noticing and listenting to our requests.
Best regards.
Edited to include link
The text was updated successfully, but these errors were encountered: