Invariant doesn't apply to declared symbols

Walter Bright newshound1 at digitalmars.com
Fri Nov 30 03:06:35 PST 2007


Janice Caron wrote:
> Fortunately, there is an answer. Well, we may not have pointers, but C
> is a reference type, right? And we do have the "ref" keyword. Thus, we
> could distinguish
> 
>     const(C) // just the data is const
>     const(ref C) // the data and the pointer are both const
> 
> That one /kind of/ works - but it does violate the principle that
> 
>     const(T) x;
>     x = y;
> 
> should always be a compile error. (If x is fully const, how can it be
> assigned?). So here's my latest and greatest idea. What about:
> 
>     const(C)ref // just the data is const
>     const(C) // the data and the pointer are both const
> 
> This would give us
> 
>     const(C) c;
>     const(C) ref d;
> 
>     c = new C: // Error;
>     d = new C; // OK
>     d.x = 100; // Error
> 
> That one works for me. It's my favorite suggestion so far. It does
> what we need, /and/ it allows const to follow a general pattern.

I think this suggestion is over-engineered. It's much simpler to say 
that a declaration, even one with const type, is rebindable unless it 
has a storage class of const.


      const C c;
      const(C) d;

      c = new C: // Error;
      d = new C; // OK
      d.x = 100; // Error



More information about the Digitalmars-d mailing list