Suggestion: "fix" assert(obj)

Kristian Kilpi kjkilpi at gmail.com
Thu Jun 14 01:55:39 PDT 2007


This issue has been proposed before. Well, I think it's the time suggest  
it again...

The problem is that

   assert(obj);

does not first check if 'obj' is null. It just executes the object's  
invariants.
So, one should usually write instead:

   assert(obj !is null);
   assert(obj);


In addition, if you write something like this

   assert(val > 0 && obj);

, then it's checked that 'obj' is not null (instead of running its  
invariants).


I propose that the two previous cases should be combined.
This won't broke any existing code. Actually, it should make it more bug  
free.

That is, if an object is used inside an assert (anywhere inside it), then  
first it's checked that the object is not null, and then its invariants  
are run:

   assert(obj);  //== "assert(obj !is null && obj.runInvariants());"

   assert(val > 0 && obj);  //== "assert(val > 0 && obj !is null &&  
obj.runInvariants());"



More information about the Digitalmars-d mailing list