Existence check for objects

Jarrett Billingsley kb3ctd2 at yahoo.com
Wed Jun 13 13:32:10 PDT 2007


"Georg Wrede" <georg at nospam.org> wrote in message 
news:467045F1.6030603 at nospam.org...
>
> Actually, why do we use the foo !is null idiom at all? (Or try to use the 
> foo != null idiom?)
>
> Anyhow, this would seem to be the canonical way to do it in a C family 
> language in the first place. So maybe we should (of course first mark 
> o==null as an error, and then) amend the documentation to suggest o itself 
> as the Politically Correct test for nullness.

Except there's one tiny inconsistency in the language.

assert(o);

This stupidly does not check that o is non-null; instead it runs the 
object's invariants.  Which, if o really _is_ null, will cause an access 
violation.  Whee.

In this case, you have to write:

assert(o !is null);

Other syntaxes to run the invariant, such as "o.invariant()" or "o.assert()" 
have been proposed, but as usual nothing has come of it. 





More information about the Digitalmars-d mailing list