[phobos] enforce() vs. assert() for range primitives

Lars Tandle Kyllingstad lars at kyllingen.net
Wed Aug 18 23:30:39 PDT 2010


On Wed, 2010-08-18 at 20:09 -0400, Michel Fortin wrote:
> Le 2010-08-18 à 19:08, Jonathan M Davis a écrit :
> 
> > The ideal situation would be to use asserts in all cases where it's going to be 
> > bugs in the program rather than bad user input and that they go away in release 
> > mode.
> 
> I'd say the ideal situation would be to have the 'in' contracts checked on the caller's side. That way a program compiled in debug mode would still check the input it sends to Phobos, and whether Phobos was compiled in debug or release mode would only affect its internal checks, not the checks about its inputs.

I wonder if it would be feasible to have two versions of the Phobos lib,
libphobos2.a and libphobos2-debug.a, distributed with DMD.  The former
would be linked in when you compile with -release, otherwise the latter
would be used.  The Phobos makefile already builds both versions, so it
would just be a small compiler change.

As for the original question, I believe assert() is the way to go with
the range primitives.  The documentation for std.range clearly states
that front() and popFront() should only be called when empty() has, or
would have, returned false, which means it should be up to the user to
make sure this is always the case.

The way things are now, with many (most?) implementations of front() and
popFront() starting with enforce(!empty), empty() is called *three*
times for each iteration of your average foreach loop.

-Lars




More information about the phobos mailing list