[OT] my experience with nullable types (C#)
Kagamin
spam at here.lot
Sun May 4 10:23:54 UTC 2025
On Saturday, 3 May 2025 at 22:42:55 UTC, Dadoum wrote:
> If your type has a nullable field, ask yourself: "When will
> this be null?" If you know that after some action this can no
> longer be null, just create a new type to encode this knowledge.
This knowledge is lost when the method returns, so you need to
refine the type on every call, so you have asserts at the
beginning.
> An example of bad design is when a lot of functions dealing
> with that object type have guards/asserts at the beginning. It
> means probably that the object doesn't provide enough
> guarantees about what it's storing.
That's how nullable types work, if you have a nullable type, it's
nullable, and you need asserts to use it. The compiler isn't
smart enough to know when it's not null.
More information about the Digitalmars-d
mailing list