On Sunday, 8 August 2021 at 11:30:41 UTC, james.p.leblanc wrote:
Hello,

With structs, I understand that "invariant checking" is called (from dlang tour):

It's called after the constructor has run and before the destructor is called.
    It's called before entering a member function
    invariant() is called after exiting a member function.

But, is is possible to have the invariant checking be performed
whenever a field is directly set?

For example, suppose a struct "S", has a field "x". I would like to
have invariance check in cases such as:

S.x = 4;

Maybe there is a hidden set field function that gets called that
might be exploitable??

Thoughts on this? Possible? Better paths that I should consider?

Best Regards,
James

You can make a field set function like so:

```
struct S
{
  private int x_;
  int x(int value) { return this.x_ = value; }
  int x() { return this.x_; }
}
```

This will then run invariants.

(boilerplate can automate that for you. https://code.dlang.org/packages/boilerplate cough self-advertisement cough)

Reply via email to