Modify MemberNotNullAttribute to be applied to constructors (add AttributeTargets.Constructor) #8831
Replies: 9 comments
-
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Beta Was this translation helpful? Give feedback.
-
Shouldn't |
Beta Was this translation helpful? Give feedback.
-
Another usecase for this would be something like public class MyWorker
{
private static Task<Dictionary<string, object>>? s_mappingTask;
public MyWorker(IService service)
{
s_mappingTask ??= service.GetMapAsync();
}
public async ValueTask<object> UseObjectFromCachedMapping(string key)
{
Dictionary<string, object> mapping = await s_mappingTask; // <-- warning CS8602: Dereference of a possibly null reference.
return mapping[key]; // no check here for demo purposes
}
}
public interface IService
{
Task<Dictionary<string, object>> GetMapAsync();
} Applying |
Beta Was this translation helpful? Give feedback.
-
use case: structs. struct can always be defaulted, so the reference type field would be null, so you'd mark these fields nullable. But it would be nice to be able to mark declared constructors as ensuring the fields are not null when these constructors were used. |
Beta Was this translation helpful? Give feedback.
-
The problem with that use case is that it falls apart immediately when you pass the struct anywhere, which is no longer sure that the struct was validly constructed. Non-defaultable struct types are a large feature on their own. |
Beta Was this translation helpful? Give feedback.
-
you may have some other way to check whether the struct has a valid value. Like in general if default is not acceptable somewhere you should check for this anyway in everything that you pass the struct to, right? and you need that even if you have nrt off. so it doesn't change anything, but allows me to say that if I use this constructor, these fields are not null. if I pass the struct to some method, the method would have to check for that case anyway if it's invalid, even if nrts didn't exist. And if that case is valid, I don't need any annotation. |
Beta Was this translation helpful? Give feedback.
-
note I have MemberNotNullWhen, and I can use that, or check for specific nullable field values, in case I got the struct from external source. |
Beta Was this translation helpful? Give feedback.
-
@333fred I think the ask is to be able to avoid an IsValid check immediately after calling a struct constructor, even though you'd have to redo the IsValid check wherever you pass the struct. (Where IsValid is a bool property with MemberNotNullWhen connected to a bunch of nullable reference properties.) |
Beta Was this translation helpful? Give feedback.
-
for my use case that's the thing. But even without such a bool property you could just check stuff for null if youdon't know whether what you get is default or not, but when you just constructed the thing then it's known that the fields are not null at the moment. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I read the issue about the MemberNotNullAttribute (dotnet/runtime#33567 & dotnet/roslyn#41438)
I don't understand why the developer is forced to write an Init() method (to which he should apply the MemberNotNullAttribute) instead of applying the MemberNotNullAttribute to the constructor itself.
If my constructor ensured the member of the class is never null why can't I apply the MemberNotNullAttribute to the contructor itself specifying the member name in the attribute?
Having a constructor that must call another method (with the attribute) looks like a nonsense to me.
I understand the CS8618 if the constructor exits and the member may not have been initialized but why can't be the constructor itself ensuring the member is not null?
Please add the AttributeTargets.Constructor to MemberNotNullAttribute/MemberNotNullWhenAttribute
Beta Was this translation helpful? Give feedback.
All reactions