NonInit

When a field is marked with @NonInit, field !is T.init is asserted.

struct NonInit

Examples

import core.time : Duration;

class Class
{
    @NonInit
    float f_;

    this(float f) { this.f_ = f; }

    mixin(GenerateInvariants);
}

(new Class(float.init)).shouldThrow!AssertError;

When **any** condition check is applied to a nullable field, the test applies to the value, if any, contained in the field. The "null" state of the field is ignored.

1 class Class
2 {
3     @NonInit
4     Nullable!float f_;
5 
6     this(Nullable!float f)
7     {
8         this.f_ = f;
9     }
10 
11     mixin(GenerateInvariants);
12 }
13 
14 (new Class(5f.nullable)).f_.isNull.shouldBeFalse;
15 (new Class(Nullable!float())).f_.isNull.shouldBeTrue;
16 (new Class(float.init.nullable)).shouldThrow!AssertError;

Conditions can be applied to static attributes, generating static invariants.

static assert(!__traits(compiles, ()
{
    class Class
    {
        @NonNull
        private static Object obj;

        mixin(GenerateInvariants);
    }
}), "invariant on static field compiled when it shouldn't");

Meta