I just got it! (invariant/const)

Janice Caron caron800 at googlemail.com
Wed Apr 9 07:59:57 PDT 2008


On 09/04/2008, Georg Wrede <georg at nospam.org> wrote:
>   void invMemberFunc() invariant; // not pure
>  What does it actually mean?

The word "invariant", in this position, means that the function's
hidden "this" parameter is typed invariant.


> A function taking no arguments, returning
> nothing? If it has no side effect,

The compiler doesn't know it has no side effects, because it's not
declared pure, so it has to err on the side of caution.


> Do we allow or disallow pure
> functions calling member functions with intra-object side-effects.

We disallow pure functions from calling non-pure functions.


>  And then it is /invariant/. What exactly does the word invariant mean in a
> function definition when it's after the function name?

See above. It means that "this" is invariant. From the caller's point
of view, it means that the member function cannot be called from a
non-invariant instance.


>  Along the same lines (please anybody explain),
>
>   int f(invariant D) invariant { ... } //omitting pure here for now
>
>  What does that mean?

Nothing. It's not legal D.


>    int f(invariant D) invariant pure{ ... }

Also not legal D. Although the following would be

    int f(invariant D d) { ... } invariant pure

And that would mean that f takes as it's parameters the hidden
invariant parameter "this" and an invariant D d. It is pure, and
returns an int.


>  If the function is pure, then should that now implicitly demand an
> invariant argument?

That's been suggested before. No reply from the Powers That Be yet.


>  Finally,
>  Since invMemberFunc is not pure, then using it in f should really be
> illegal.

Yes


>  (1) Which leads to the tought: since a pure function can't call other
> functions to use their side effects, the only reason left to call a function
> is to get it's value. From which follows that calling void functions should
> be made illegal!

Well, they can be optimised away, at least.



More information about the Digitalmars-d mailing list