[Issue 18757] static invariant{} should either work or not be valid syntax

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Apr 16 12:42:26 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=18757

RazvanN <razvan.nitu1305 at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |razvan.nitu1305 at gmail.com

--- Comment #1 from RazvanN <razvan.nitu1305 at gmail.com> ---
(In reply to FeepingCreature from comment #0)
> Right now, you can define a struct to have a static invariant:
> 
> struct S {
>   static invariant { assert(false); }
>   static void foo() { }
> }
> 
> But the invariant will not be checked when calling foo.
> 
> Either this should work, or "static invariant {}" should be a syntax error.

Placing static in front of an invariant has no effect on the invariant. The
problem with the above code is that the method foo is marked as static:

struct S
{
    invariant { (assert (false); }
    static void foo() {}
}

void main()
{
    S s;
    s.foo(); \\ invariant does not get called
}

while :

struct S
{
    static invariant { (assert (false); }
    void foo() {}
}

void main()
{
    S s;
    s.foo(); \\ invariant does get called
}

So we can conclude that invariants are not called for static functions. The
spec does not mention anything about this so there are 2 possibilities :

1. Invariants were designed only for struct/class instances (in which case the
bug report will be closed with a spec update)

2. Invariants should also be called when static methods are called (in which
case this should be fixed)

--


More information about the Digitalmars-d-bugs mailing list