logical const is a subset of transitive const

Steven Schveighoffer schveiguy at yahoo.com
Fri Sep 14 14:00:54 PDT 2007


"Janice Caron" wrote
> What I /don't/ understand is why D lets you declare a member function
> (as opposed to an object) as being invariant. To my brain, that ought
> to mean the function sees "this" as having the type invariant(T) - but
> that's clearly an invalid assumption, because "this" /can/ be
> modified. You have a read-only view, that's all.
>

Yeah, I totally agree that invariant declared on a function doesn't seem to 
be right.  I'm guessing the only reason they did that is because it makes no 
sense to return an invariant type, so you could put invariant before the 
function name and not be ambiguous.

i.e.

const int* f(); // is f const, or is it returning a const int*?
invariant int* f(); // can't have a pointer to invariant (at least in 
current spec), so invariant must refer to f

If that is the reason, then I say use const, and put it after the func 
declaration, like in C++.  May look weird, but it's unambiguous.

> "Pure" on the other hand, only makes sense for functions. So far as I
> know, there's no such thing as a pure object.

pure implies that:
1. you can only call pure functions
2. you can only access invariant data (data that cannot be changed anywhere 
else).

so I think you are right, pure is more for functions.

BTW, if we get rid of invariant for functions meaning the same thing as 
const functions, I think pure should be replaced with invariant.  It makes 
more sense to me as an intuitive keyword.  An invariant function cannot 
change anything and can only access invariant data, and call other invariant 
functions.  Makes a lot of sense to me...

-Steve 





More information about the Digitalmars-d mailing list