[OT] OT: Null checks.
Max Samukha
maxsamukha at gmail.com
Wed May 7 07:42:06 UTC 2025
On Tuesday, 6 May 2025 at 23:51:05 UTC, Walter Bright wrote:
> 2. if in invalid state, shut down immediately
There's one case where D blatantly violates this principle.
Consider a system with allowed state transitions A->B, B->C,
C->B, B->A:
```
enum State
{
A, B, C
}
State state;
void AtoB()
{
assert(state == State.A);
state = State.B;
}
void BtoC()
{
assert(state == State.B);
state = State.C;
}
void CtoB()
{
assert(state == State.C);
throw new Exception("CtoB failed"); // or an Error, doesn't
matter
state = State.B;
}
void BtoA()
{
assert(state == state.B);
state = State.A;
}
void main()
{
AtoB();
scope(exit)
BtoA(); // relies on the downstream handler having
completed successfully.
BtoC();
scope(exit)
CtoB(); // this handler runs, even though the program is
in an invalid state.
}
```
More information about the Digitalmars-d
mailing list