Replies: 20 comments
-
Glad to see that |
Beta Was this translation helpful? Give feedback.
-
I've got mixed feelings about |
Beta Was this translation helpful? Give feedback.
-
You can. Note that you will get a warning if you actually pass a possible-null value to a non-nullable location - the compiler still maintain the actual state of the variable. Also, since that is only concerning reassignments, it would make sense to NOT do that for "readonly locals" and make them infer nullability from the init expression. |
Beta Was this translation helpful? Give feedback.
-
@alrz My point is that usually I want the LHS location to be a non-nullable one, and I want to get the warnings when I am assigning nullable values to that location, not when I am assigning the value from this location to a non-nullable location. |
Beta Was this translation helpful? Give feedback.
-
I'd humbly propose:
|
Beta Was this translation helpful? Give feedback.
-
Where/when is the concluded |
Beta Was this translation helpful? Give feedback.
-
The The point at which nullability really matters is when a method call is made on the variable, when it is passed into another method or when it is returned, at which point flow analysis would determine whether it was safe or not. If you don't care to make a distinction between To be clear, I agree with your position on Here is another similar situation aside from public void SomeMethod(object p1, object p2)
{
if (p1 is string name) {
// ...
}
else {
if ((name = p2 as string) == null) { // error converting null value to non-nullable type
throw ...
}
}
} The syntax |
Beta Was this translation helpful? Give feedback.
-
The other sensible option would have been to make |
Beta Was this translation helpful? Give feedback.
-
Soon. @jcouv? |
Beta Was this translation helpful? Give feedback.
-
@mikernet We had the discussion about whether to let maybe-null values into locals, and decided against it. However, those produce a distinct warning (CS8600) which can easily be suppressed for those who don't care about the discipline/readability/intent this provides. We decided against As for the type inferred for |
Beta Was this translation helpful? Give feedback.
-
I do not think your argument against Changing #nullable enable
class C
{
string NotNullable()
{
var s = (string)""; // <- ignores the explicit cast and makes it string? regardless
s = Nullable();
return s;
}
string? Nullable() => "";
} At least that way I can work around this unfortunate change for the worse by overriding the new default behaviour with a cast. |
Beta Was this translation helpful? Give feedback.
-
As a quick recap we considered the following options (at length), and maybe some others that I forgot:
You're promoting some flavor of option (2). I used to as well ;-)
Yes, there is a downside of the approach we selected (we don't warn on this assignment, and only warn later, with a safety diagnostic). If I could, I would prefer to avoid it, but the trade-off was judged better than other options.
Sure, we could do that, but to what end or benefit? |
Beta Was this translation helpful? Give feedback.
-
That's fantastic. I've been playing with silenced The only thing that I think would really nicely polish off this feature is a very subtle visual indicator for the null flow state of your values, which would make local nullability inferred/dynamic but readily visible...IMO the best of both worlds, so looks like I'm doing up an extension for that 😃 |
Beta Was this translation helpful? Give feedback.
-
This behavior feels like a regression with public void MyMethod()
{
var callLog = new List<string>(3);
// do stuff with with NestedMethod etc.,
void NestedMethod(string s) => callLog.Add(s);
} Previously the Looks like that was the behavior as well if you declared |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Curious, you get the warning if you comment out the call to the local function. |
Beta Was this translation helpful? Give feedback.
-
@agocke In addition to what Halo said, you also forgot to place |
Beta Was this translation helpful? Give feedback.
-
Yeah, even with |
Beta Was this translation helpful? Give feedback.
-
That said, if the local function is unreachable you can also delete it :) |
Beta Was this translation helpful? Give feedback.
-
@agocke, this is the more complex example that case of what @HaloFour (which I originally asked about on Gitter): [Fact]
public async Task Test001()
{
var callLog = new List<string>(3);
var cut = RenderComponent<FixtureComponent>(builder => builder
.Add(p => p.Setup, MyLocalMethod)
.AddChildContent("FOO")
);
// callLog assertion
void MyLocalMethod(FixtureComponent fixture) => callLog.Add(nameof(MyLocalMethod));
} The complete test is over in my repo. So in this case where I get the warning, I pass |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
https://github.com/dotnet/csharplang/blob/e4b05fb2cad47ea67c5b4dcb46bf4143895d571f/meetings/2019/LDM-2019-12-18.md
Agenda:
var?
Beta Was this translation helpful? Give feedback.
All reactions