Const sucks

Bill Baxter dnewsgroup at billbaxter.com
Mon Sep 10 13:16:55 PDT 2007


Derek Parnell wrote:
> On Mon, 10 Sep 2007 12:15:09 -0700, Walter Bright wrote:

>> o  tail const of a struct would have to be done by making the struct a 
>> template:
>>
>>    struct S(T) { T member; }
>>    S!(int)   // tail mutable
>>    S!(const(int)) // tail const

So then it would be ok to completely overrwrite an S, but not to set 
it's only member directly?
alias S!(const(int)) T;
T a;
T b;
a = b;  // ok??
a.member = b.member; // not ok??

It seems a little odd since they do exactly the same thing.
Hopefully copying a struct will be treated the same as elementwise 
copying of the members (so both should be illegal above).

> But most structs contain multiple members, and usually of different types.

Probably someone clever will create a type-constructor template that 
generates constified version of a given struct type using __traits or 
something.  Then you'll just do

    alias MakeConst!(MyStruct) MyConstStruct;

If not, we'll just keep pestering Walter until it is possible to write 
such a template. :=)

Probably more realistic is a case where you want to switch between
struct T { int* x; int y; }
and
struct T { const(int)*; int y; }

So the const doesn't start on the member level, but 
stuff-pointed-to-by-members level

--bb



More information about the Digitalmars-d mailing list