Replies: 5 comments 38 replies
-
I guess once we have the required initializer this should be solved. |
Beta Was this translation helpful? Give feedback.
-
The reasoning is that declaring a property to be non-null and initialization at the construction site are two orthogonal concerns. |
Beta Was this translation helpful? Give feedback.
-
It is because public record R
{
public string X { get; init; }
} you'll see that it still declares a The C# team take a iterative approach to adding such features. As @acaly says, the next step in the process is to add some sort of "required properties" feature: public record User
{
public string FirstName { get; required init; }
public string LastName { get; required init; }
public string EmailAddress { get; required init; }
} At which point, you should no longer get those new User { } If this seems a back to front approach, welcome to the whacky world of C# where "hills of success" are favoured over "pits of success" all too often. 🤷 |
Beta Was this translation helpful? Give feedback.
-
TLDR: records and NRTs are 100% orthogonal. NRTs are both optional and designed very intentionally to have no effect on the behavior of the code other than to generate diagnostics. Records need to compile and behave identically with or without NRTs enabled. Required properties should alleviate some of the issue here as that should move the warning to the site of consumption. Required properties would give you a warning if you do not set the property, and NRTs, if enabled, would give you a warning if you set the property to a possibly-null reference. The compiler could then drop the diagnostic at the declaration site. |
Beta Was this translation helpful? Give feedback.
-
If that's the meaning you took, then I expressed myself poorly. What I've been trying to say is "These two features are not the same feature, and so please stop asking for them to be the same feature." @acaly, you wrote:
This reads to me as trying to get the compiler to enforce some kind of requiredness as a part of the nullability feature - trying to bring in requiredness by stealth. The nullability (or not) of a property cannot be used to reliably infer the requiredness (or not) of that property. I understand that you have a very common scenario where nullability and requiredness are very closely related. There are a lot of people who have that scenario, and this is why the C# LDM are designing/implementing requiredness. But, and this is important: There are valid, useful, and common scenarios where a non-nullable property is not required. The CS8618 warning is correct - and like every other warning in C#, it's up to the developer to decide whether to fix it (warnings are, after all, not errors), and further how to fix it. The warning is telling the developer that there's something missing here. Either the constructor needs to be modified to assign a value, or it needs to be assigned during initialization, or the warning can be suppressed, or the warnings can be ignored. |
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.
-
Coming from F#, this doesn't make much sense to me:
It seems that if you have non-nullable fields, the only way to make this right is to use a constructor, which then disallows object initialization syntax, and brings it half way back to just being a class with readonly fields again (except it allows using with to do updates).
In F#, you receive an error if in the object initialization you don't specify all fields. That way there's no reason for it to warn about unassigned non-nulls.
What's the reasoning behind this weird, seemingly self-defeating behavior? Am I doing something wrong?
Might it make sense to remove the CS8618 warning for record fields when the record has no constructor, and instead warn if in the object initialization, a (non-nullable) property isn't assigned to?
Beta Was this translation helpful? Give feedback.
All reactions