Open question: what code pattern you use usually for null safety problem

Adam D. Ruppe destructionator at gmail.com
Thu Jan 14 19:24:54 UTC 2021


On Thursday, 14 January 2021 at 18:24:44 UTC, ddcovery wrote:
> This is only an open question to know what code patterns you 
> usually use to solve this situation in D:

I'm almost never in this situation except for reading things like 
xml or json data that may be missing.

So I just special cased those. My json lib doesn't return null 
per se, it returns var(null) which is allowed to just return more 
harmless nulls. Thus you write 
`person.father.father.name.get!string` and it will be empty if 
anything was null in the chain.

With dom, you can optionSelector("person > father > father > 
name").innerText and again if the selector returned null, all its 
methods also just return empty strings or whatever.

So the library handles these special cases and then I don't worry 
about nested nulls anywhere else since I consider it bad style to 
even be in that situation in the first place.


More information about the Digitalmars-d-learn mailing list