I just got it! (invariant/const)

Georg Wrede georg at nospam.org
Wed Apr 9 09:11:42 PDT 2008


Joining Janice's two replies.

Janice Caron wrote:
> 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.

Ok.

>>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.

Ok.

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

Ok.

>> 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.

Ok.

>> Along the same lines (please anybody explain),
>>
>>  int f(invariant D) invariant { ... } //omitting pure here for now
>>
>> What does that mean?
> 
> My apologies. Of course it's legal. I was misreading. (Although you
> did miss out the variable name for D. Let's call it d)

Right.

> So it means that "this" is invariant, and that d is invariant.

To be precise, it /requires/ that "this" is invariant, right?

>>   int f(invariant D) invariant pure{ ... }
>
> Also I screwed up. Yes, that is legal D. My apologies again. It means
> as above, plus the function is also pure.
> 
>     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.

Which is precisely the same as the previous one?


Additionally, I seem to remember that stuff like

kljal alsje fss laskef(kajs aks) {

   // potentially hundreds of lines here

} lksjd afaf

was frowned upon in D. Has this changed? The point being, it's bad to 
have some part of the signature geographically remote from the rest.

>> 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.

Maybe it's too obvious to even comment upon? :-) I mean, what if it's 
not implicit? Then folks doing pure functions would have to write 
invariant all over the place, *PLUS*, any newcomer would take it for 
granted that since you have to write it, then there *can* be pure 
functions not taking invariants! So, IMNSHO, there's no other way than 
to make them implicit, and therefore forbidden as redundant.

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

Good.

>> (1) Which leads to the thought: 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.

They can be flatly removed! If they don't return a value, and don't 
change anything, what's left?!

Actually, to be more precise:

  - A pure function can only call other pure functions.
  - Any pure function has to return a value.
  - In a pure function, ignoring the return value of a called function 
is an error.

This is even stronger than (1) above, it is clearer,
and it's *right*.



More information about the Digitalmars-d mailing list