Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@plioi
Copy link
Contributor

@plioi plioi commented Dec 16, 2023

When nullable reference types were first enabled, the PipeMessage message types presented an annoyance. Their setters were naturally public { get; set; }, though we wanted to declare the property types as non-null. Careful tracing of code paths confirmed that by the time they would be inspected, they really would be non-null. Having to mark them with ? would have needlessly spread unrealistic warnings to their usages, so instead we marked them all with the "confident assertion" { get; set; } = default!.

Since then C# has added two useful keywords which better help express and enforce the intention of these "serializable property bucket" types. Here we apply both of them:

  1. init as an alternative to set, communicating that they are intended to be populated during construction/initialization.
  2. required as an alternative to = default!, communicating that there is no need for a weak nullability assertion as the value will verifiably be assigned non-null upon initialization.

…del properties, as the method's reliance on the `new()` constraint is incompatible with applying `required`.

This entire change was performed by an automated refactoring.
… their intended use is to be fully populated upon construction and never changed.
…e set either in constructors or object initializer, instead of using the older fallback assertion `= default!`, allowing the compiler to vet that assertion.
@plioi plioi marked this pull request as ready for review December 16, 2023 16:14
@plioi plioi merged commit 399af25 into main Dec 16, 2023
@plioi plioi deleted the pipe-message-nullability branch December 16, 2023 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants