August 09, 2021

I have a struct where I use a number of

"invariant(){enforce( .... blah, blah ...);}"

statements in the struct body to enforce certain
conditions on a struct member.

Since these "invariants" are only called when struct member
functions are exercised, must I overload each
individual operation that may lead to a violation?

Or, is there any way to funnel all such operations, so my
invariants are exercised?


For example, struct member "x" is an integer slice,
I can account for some of the operations that affect the
slice slice via (such as the append operator):

>

// overloaded to activate enforcements
void opOpAssign(string op)(int a){mixin("this.x"~op~"= a;");}
void opOpAssign(string op)(int[] a){mixin("this.x"~op~"= a;");}

(since these are picked up as member function, they can
exercise my "invariant").


But there are a number of slicing operation that I also
need to be careful of.

I must disallow any operation that changes the
length of my slice, or the pointer associated with my
slice.

How should I account for all combinations of operations
that might change size of slice member?

Any elegant solutions here?

Best Regards,
James