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

ddcovery antoniocabreraperez at gmail.com
Thu Jan 14 18:24:44 UTC 2021


I know there is other threads about null safety and the 
"possible" ways to support this in D and so on.

This is only an open question to know what code patterns you 
usually use to solve this situation in D:

   if(person.father.father.name == "Peter") doSomething();
   if(person.father.age > 80 ) doSomething();

knowing that *person*, or its *father* property can be null

i.e.: the incremental null check solution

if(
   person !is null &&
   person.father !is null &&
   person.father.father !is null &&
   person.father.father.name == "Peter"
)
{
   doSomething();
}

or the "monad" way

[person].
   filter!"a !is null".map!"a.father".
   filter!"a !is null".map!"a.father".
   filter!"a !is null".map!"a.name".
   each!( (name) {
     if(name == "Peter") doSomething();
   });

or, may be, you have some helper function/structs/templates

if( person.d!"father".d!"father".d!"name".get == "Peter" ){
   doSomething()
}
if( person.d!"father".d!"age".get(0) > 80 ){
   doSomething()
}

or an "xml path" like template

if( person.get!"father.father.name" == "Peter" )
if( person.get!"father.father.name.length"(0) == 5 )
if( person.get!"father.father.age"(0) > 80 )


If it's not a bother, I'd like to know how you usually approach it

Thanks!!!




More information about the Digitalmars-d-learn mailing list