Suggestion: "fix" assert(obj)
Kristian Kilpi
kjkilpi at gmail.com
Thu Jun 14 13:07:18 PDT 2007
On Thu, 14 Jun 2007 11:55:39 +0300, Kristian Kilpi <kjkilpi at gmail.com>
wrote:
>
> 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());"
Well, actually, if the object's invariants will be executed whenever the
object is used as an boolean value inside an assert, it *could* break
existent code: some asserts could fail when they shouldn't. Ok, that
should not happen commonly though. Hm, maybe some 'obj.invariantAssert()'
syntax should/could be used instead. I don't know.
More information about the Digitalmars-d
mailing list