On Monday, 15 October 2012 at 15:56:33 UTC, Steven Schveighoffer wrote:

Isn't it possible to customize when an "invariant" is called using contracts?

For example:

struct S
{
   private bool isValid;
   private void _invariant() {assert(isValid);}

   void foo()
   in { _invariant();} out {_invariant();} body
   {
      // whatever
   }

   void opAssign(ref S other)
   out {_invariant();} body
   {
      isValid = other.isValid;
   }
}

???

Yeah, It's extra work. But essentially, isn't this what you want? The thing about disabling invariant checks on some specific function in some specific case is that someone else has a valid case for requiring it.

The only sucky part about the above is, _invariant is compiled in even in release mode (though it should inline to a noop).

-Steve

There is now an "assert" word for version blocks so you can put your code inside that, and it doesn't get compiled in during release.

Reply via email to