Fully transitive const is not necessary

Steven Schveighoffer schveiguy at yahoo.com
Thu Apr 3 07:51:05 PDT 2008


"Walter Bright" wrote
> If you do away with transitive const, you cannot have invariant either. 
> Const allows you to reuse code that works on invariants to work with 
> mutables, too.

I'm not "doing away with" transitive const.  I'm saying allow the exception 
that already exists as global variables to be easier to implement.  I still 
believe that the default meaning of const is transitive.  This is unlike 
C++'s const.  The exception is when you declare a piece of a class to be 
mutable, it is now declared to be not part of the state of the class, but a 
piece of data associated with the class, just like a global AA would 
associate a class (the key) with some data (the value).

> Logical const just utterly breaks the whole system. Every scheme we've 
> looked at for logical const winds up in some way breaking invariants. If 
> invariants are broken, the advantages of them all just go away. I suspect 
> that logical const is like perpetual motion - if you think you've solved 
> it, you've just made a mistake somewhere <g>. I also suspect that the 
> failure of logical const validates the D scheme of invariants as being 
> correct - there shouldn't be any holes in it.

Think of the mutable portion of logically invariant classes as global 
variables, but with better protection capabilities.  And saying "logical 
const winds up in some way breaking invariants" doesn't make it true :)  In 
fact, it is false.  I have proven that logical const is already possible. 
You have not pointed out any mistakes I have made.  History is full of 
examples where people were absolutely sure that something was true, but they 
turned out to be wrong.  I have offered proof for my views.  You have 
offered anecdotes and analogies.  Please try harder :)

> You're right that invariant by itself is not enough to specify a pure 
> function, a pure function also cannot read or write global state. But 
> invariant is still a necessary condition, it's just not sufficient.

Exactly, since the mutable portion is not part of the object state, it 
should be inaccessible to pure functions.

>
> Peoples' troubles with const seem to stem from attempting to defeat it in 
> one way or another. While defeating const is legal and trivial in C++, D 
> tries to close those doors.

I'm not trying to defeat the const system.  I'm showing you that the const 
system already supports "operationally equivalent" logical const.  Since it 
already supports an equivalent version of logical const, why not support it 
directly, and make coders lives easier who want to use it.

I can come up with a set of rules for logical const that allow pure 
functions which the compiler can statically verify that the function has no 
side effects and is not affected by any other function's side effects.  I 
think this is the goal, is it not?

-Steve 





More information about the Digitalmars-d mailing list