Replies: 10 comments
-
The current idea is that I'm also not sure how I feel about the other example, because that declaration would then all of a sudden become a |
Beta Was this translation helpful? Give feedback.
-
This is probably reasonably common. Any time you're re-using a variable in a potentially complicated method and want to clear it out at some point (if some condition gets met, for example). |
Beta Was this translation helpful? Give feedback.
-
Hm, even if the compiler always treated the reference type inferred as nullable the compiler should still be able to determine it's actual nullness based on flow analysis. So it makes sense for string ReturnsNonNullable() => "Hello";
string? ReturnsNullable() => "World";
var s = ReturnsNonNull(); // is string? but is definitely not null
if (s.Length > 0) { ... } // no warning, s is not null here
s = ReturnsNullable(); // legal, s is nullable
if (s.Length > 0) { ... } // warning, s might be null here
s = null; // legal, s is nullable I guess it depends on if it's considered more important to guard the value of |
Beta Was this translation helpful? Give feedback.
-
I think not. If I opted into non-nullable references, I want to get a warning at |
Beta Was this translation helpful? Give feedback.
-
Why? Assignment to a local is irrelevant if you don't do anything with that local and if you get the warning at any attempt to use that local then non-nullable references would still be doing it's job. I'm not really arguing one way or another, just trying to understand the arguments. Since non-nullable references aren't types but only hints to usage and a specific variable may switch between nullable and non-nullable due to flow analysis (when declared as nullable) then the requirement of |
Beta Was this translation helpful? Give feedback.
-
I've also copied my original comment here as I think it's more relevant to that thread. |
Beta Was this translation helpful? Give feedback.
-
Once this null analysis takes off, people might see it as more than a subtle hint, but rather the replacement of the null-handling in their code. Using var or not using var is way too trivial and should not change the null-safety of your code. |
Beta Was this translation helpful? Give feedback.
-
I see where you're going with this. To be fair, that makes it easier to adopt. At the local level you should mainly depend on static analysis as a consumer of |
Beta Was this translation helpful? Give feedback.
-
I agree with @alrz that C# should infer the type as non-nullable whenever possible. Otherwise these two snippets will get different treatment from the compiler:
Yes, flow analysis might infer that |
Beta Was this translation helpful? Give feedback.
-
That was my first impression too. But you should not look at In that example |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
var? keyword might be useful when we introduce nullable reference types.
For example
It might be useful for nullable struct too.
insted of
Beta Was this translation helpful? Give feedback.
All reactions