Automatic invariant generation

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Fri Jul 7 19:06:40 PDT 2017


On Friday, July 7, 2017 2:31:42 PM MDT Nicholas Wilson via Digitalmars-d 
wrote:
> On Friday, 7 July 2017 at 14:17:34 UTC, Jonathan M Davis wrote:
> > What does it even do?
>
> asserts that the this pointer is not null, apparently which is
> annoying because you'd crash anyway. I suppose you might get a
> nicer error message but it doesn't add much.
>
> > I don't see how it makes any sense for _anything_ to have an
> > invariant if it's not explicitly declared.
>
> Worse I can't even @disable it because thats a syntax error.
>
> > And honestly, I'm of the opinion that invariants with structs
> > are borderline useless, because they're run even before
> > opAssign, meaning that if you ever need to use = void; or use
> > emplace, then you're screwed if you have an invariant, because
> > it's bound to fail due to the object not having been
> > initialized previously.
>
> Huh, I didn't know that.

I was not pleased to find out about it either, and the result is that I tend
to think that invariants have no business being in structs, much as it would
be desriable for them to be there.

> That does seems to be purpose defeating zealotry.

It's desirable when the object is supposed to be in a good state, because
then you know that when you do the assignment, you'll catch if something
broke the invariant before the assignment. But it's completely undesirable
when the object was purposely uninitialized, and since you can't choose
whether the invariant is run or not, IMHO, _not_ running it would be better,
but I was not persuasive enough:

https://issues.dlang.org/show_bug.cgi?id=5058

I was discussing this issue with someone at dconf, and as a result of that
conversation, I've considered writing a DIP that would allow you to
explicitly skip calling an invariant on a specific assignment (since in
theory, you should know when you're assigning to an unitialized object), but
I haven't had the time to think it through completely, let alone put
together a DIP that might actually be persuasive.

- Jonathan M Davis



More information about the Digitalmars-d mailing list