Final, Const, Invariant

Lutger lutger.blijdestijn at gmail.com
Fri Mar 23 07:26:54 PDT 2007


David B. Held wrote:
> Since the Extended Type Design thread has blown my stack (and taken all 
> the heap in my mail reader), here is my explanation of these concepts 
> for anyone who might still be confused.  Yes, the explanation is 
> cartoony, but I hope it helps:
> 

Haha thanks, only know I understand it clearly, time to read some of 
that thread again...

It is much clearer though than C++, for there it depends on where you 
put the const which kind of meaning it has.

To sum it up, there are three kinds of 'immutability', where one (const) 
is a subset of another (invariant). With this, we can control the 
semantics we want precisely and combine them to get a grand total of 
five different meanings:

final Object x = y; // x won't ever reference anything but y
const Object* x = &y; // y can't be changed by x
invariant Object x = new Object; // x can't be changed at all
final const Object* x = &y; // x points to y forever and can't change y
final invariant Object* x = &y; // same as above but y will never change

This is right?



More information about the Digitalmars-d mailing list