Final, Const, Invariant

Dan murpsoft at hotmail.com
Fri Mar 23 11:02:31 PDT 2007


Andrei Alexandrescu (See Website For Email) Wrote:

> Lutger wrote:
> > 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?
> 
> Yah.
> 
> Andrei

I think the concept is right.

Details, thoughts on which word ought to mean which?  I agree we should use three keywords to accomplish the storage class thing.  Note though that const and invariant are already keywords, and this might break some things...

I'm still curious though Andrei, what about Object*[32][char[]], where we want to ensure the char[]'s are invariant?

Is that Object*[32][invariant char[]] ?

Also, if this gets implemented, enough needs to be explained so we can use it correctly for all the odd uses.  it will have to go in the guide, mm?

Can I use static with that?  Do I need to?  If something's invariant, is it implicitly static?

So can I use myFunc( invariant char[], int x) ?  What does that mean, that it won't change at all during the function operation, that it only accepts invariant char[]'s, or what?

Can I use it in a tuple, template, mixin, and alias function/delegate?

That needs to be decided now I think.



More information about the Digitalmars-d mailing list