Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
March 23, 2021 Manually check struct invariants | ||||
---|---|---|---|---|
| ||||
For a class object obj, one can use assert(obj) to get its invariants checked. How to do this for structs? |
March 23, 2021 Re: Manually check struct invariants | ||||
---|---|---|---|---|
| ||||
Posted in reply to Q. Schroll | On Tuesday, 23 March 2021 at 22:22:12 UTC, Q. Schroll wrote:
> For a class object obj, one can use assert(obj) to get its invariants checked. How to do this for structs?
It's called after the constructor has run and before the destructor is called.
It's called before entering a member function and after exiting a member function.
So technically you could just do like obj.writeln or whatever u have 😁
|
March 23, 2021 Re: Manually check struct invariants | ||||
---|---|---|---|---|
| ||||
Posted in reply to Q. Schroll | On Tuesday, 23 March 2021 at 22:22:12 UTC, Q. Schroll wrote: > For a class object obj, one can use assert(obj) to get its invariants checked. How to do this for structs? https://dlang.org/spec/expression.html#assert_expressions > If the first AssignExpression is a pointer to a struct instance for which a Struct Invariant exists, the Struct Invariant must hold. So, you would write `assert(&obj)` for a struct instance. |
March 23, 2021 Re: Manually check struct invariants | ||||
---|---|---|---|---|
| ||||
Posted in reply to Imperatorn | On 3/23/21 4:14 PM, Imperatorn wrote:> On Tuesday, 23 March 2021 at 22:22:12 UTC, Q. Schroll wrote: >> For a class object obj, one can use assert(obj) to get its invariants >> checked. How to do this for structs? > > It's called after the constructor has run and before the destructor is > called. > > It's called before entering a member function and after exiting a member > function. > > So technically you could just do like obj.writeln or whatever u have 😁 Just to make sure everyone knows "it" is the invariant block: struct S { int i; int j; this(int i, int j) { this.i = i; this.j = j; } invariant() { assert(i == 2 * j); } } void main() { auto s = S(1, 10); // Won't pass the invariant check } Ali |
March 24, 2021 Re: Manually check struct invariants | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | On Tuesday, 23 March 2021 at 23:27:54 UTC, Paul Backus wrote:
> On Tuesday, 23 March 2021 at 22:22:12 UTC, Q. Schroll wrote:
>> For a class object obj, one can use assert(obj) to get its invariants checked. How to do this for structs?
>
> https://dlang.org/spec/expression.html#assert_expressions
>
>> If the first AssignExpression is a pointer to a struct instance for which a Struct Invariant exists, the Struct Invariant must hold.
>
> So, you would write `assert(&obj)` for a struct instance.
I searched for it, but while for some reason, my brain read "If the first AssignExpression is a reference to a class instance for which a Class Invariant exists, the Class Invariant must hold." it failed to do so for the next sentence.
Thank you.
|
Copyright © 1999-2021 by the D Language Foundation