Why can't we define re-assignable const reference variable?

Yigal Chripun yigal100 at gmail.com
Wed Feb 20 13:22:00 PST 2008


what if we separate const into two keywords?
a) const would apply to data only. (transitively)
b) "bound" ( a new keyword ) would apply to references only  (transitively)

the idea is as follows:
Class c1 = new Class(); //all mutable
const Class c2 = new Class(); // mutable ref to const data
same as:
const(Class) c3 = new Class();
bound const Class c4 = new Class(); // bound (ie const) ref to const data
const int a; // a is const
bound int a; // illegal
bound Class c5 = new Class(); // const ref...
etc...
that's just a start of a start of an idea.
it could be developed much further... like where the "invariant" concept
would fit in with such a scheme.

-- Yigal

Janice Caron wrote:
> On 20/02/2008, Sergey Gromov <snake.scaly at gmail.com> wrote:
>   
>>  As I understand from your posts, you state that "const(C) x;", as well
>>  as "const C x;", declares a variable of type (const C).  But this is not
>>  true.  It actually declares a variable of type "const ref to const C".
>>     
>
> No. Consider
>
>     const(int) x;
>
> This does not declare a variable of type "const ref to const int", it
> declares a const int.
>
> I understand that you're thinking of classes, but even with classes,
> you're not quite getting what a class is. A class is /not/ the data on
> the heap. (If that were so, it would be possible to create an array of
> dereferenced class instances, and it isn't). No, a class /is the
> reference/ - and indirectly, what that reference points to. Thus, a
> const class /is/ a const reference.
>
> A "tailconst" class, on the other hand, would be a class (by which I
> mean, a reference) whose tail (the data on the heap) was const.
>   



More information about the Digitalmars-d mailing list