It's worse than I thought

Walter Bright newshound1 at digitalmars.com
Mon Dec 3 03:00:44 PST 2007


Sönke Ludwig wrote:
> I've noticed some things that seem to be inconsistent with this behavior 
> for local variables and where I've not found a workaround for (with the 
> exception of casts):
> 
> 
> 1. Arrays or associative arrays of references to const instances:
>   class C {}
>   const(C)[] a;
>   const(C)[string] b;
> 
>   It seems to be impossible here to make the array elements assignable, 
> while at the same time keeping the class instances const. I currently 
> need this in a function, where I'm getting a const array of instances 
> and need to build a map to those.
> 
>   void fn( in C[] objs ){
>     const(C)[string] map;
>     map["test"] = objs[0]; // error, not mutable
>   }
> 
> 
> 2. Class references as members:
>   class D {
>     const(C) c;
>     const C d;
>     this( C c ){
>       this.c = c; // error
>       this.d = d; // works, but, of course, only inside of the constructor
>     }
>   }

There isn't much to be done about this problem except make the 
implementation of the class itself const.



More information about the Digitalmars-d mailing list