On Tuesday, 17 January 2017 at 15:59:26 UTC, Andrei Alexandrescu wrote:
On 1/17/17 12:08 PM, Mark wrote:
On Tuesday, 17 January 2017 at 09:17:56 UTC, Andrei Alexandrescu wrote:
On 1/17/17 9:32 AM, Eugene Wissner wrote:
Ah, well thanks. I don't think it makes much sense since it would be easier to write a complete setter if the user needs extra checks. Accessors are there only for the generation of the standard methods,
that just get or set some object property.


Hmmm... that's a bit of a bummer because it helps only the degenerate case (accessors are there as placeholders for future extensions, and otherwise offer no protection whatsoever compared to a public value). The question would be then what would be use cases for the accessors. Predicated setters are not just a random thing one might want out of
many possibilities, it's a frequent pattern. -- Andrei

Given that D supports class invariants, is there a real need for
predicated setters?

The invariant is evaluated after the setter has taken place, i.e. after the object has been corrupted. The setter guard prevents corruption from happening. -- Andrei

I see. Is there a way to call invariant() of a class/struct directly? That would obviate the need for a particular predicate (copy the class state, run the setter, check if invariants are satisfied and restore previous state if they aren't).

Reply via email to