Very confusing error message when calling a class method from an invariant

Meta jared771 at gmail.com
Wed Mar 10 03:39:15 UTC 2021


class Human {
     static immutable MAX_AGE = 122;

     bool alive = true;
     int age = 0;
     //Error: mutable method onlineapp.Human.checkAge is not 
callable using a const object
     invariant(checkAge());

     void growOlder()
     in(alive)
     out(; checkAge())
     {
         age++;
         if (age > MAX_AGE)
             die();
     }

     void die()
     in(alive)
     out(; !alive) {
         alive = false;
     }

     bool checkAge() {
         return age >= 0 && age <= MAX_AGE || !alive;
     }
}

void main() {
     Human h = new Human();
     h.growOlder();
}

What the hell does this even mean, and where does it come from? 
Adding `inout` to `checkAge` actually does cause it to compile 
and run too. WTF?


More information about the Digitalmars-d-learn mailing list