using "invariant" with structs ... possible to call when a field value is set??

james.p.leblanc james.p.leblanc at gmail.com
Sun Aug 8 12:03:14 UTC 2021


On Sunday, 8 August 2021 at 11:36:51 UTC, FeepingCreature wrote:

>
> 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)

FC,

Thanks! This is exactly what I had hoped might be possible!

I had made some naive "newbie-D" attempts at something like this
be couldn't get anything to work.  Thanks for providing an exable
of the exact code that would work.  Much obliged!

Best Regards,
James

PS Even more important than solving my immediate problem, I have
gained a better understanding of the struct and its member 
functions.





More information about the Digitalmars-d-learn mailing list