const by default.

kris foo at bar.com
Fri Jul 7 10:51:43 PDT 2006


Walter Bright wrote:
> Const has a number of issues whether or not it is default:
> 
> 1) function parameters that are reference types (what is talked about 
> most here) being const
> 2) member functions being able to modify the object instance (const 
> functions)
> 3) const in the return value
> 4) what happens if both const and non-const references to the same data 
> are simultaneous, and one modifies through the non-const one?
> 5) assignment of a const reference to a non-const one, either explicitly 
> or through a function parameter
> 6) what happens if one returns a const reference
> 
> One way to do it is to have const-as-type-modifier like C++, something 
> I've tried to avoid as being excessively complex (compiler and 
> programmer) and ugly.
> 
> Another way I've been toying with is just making the 'in' parameter 
> storage class imply const reference:
> 
> class C { int m; }
> 
> void bar(C c) { c.m = 3; } // ok
> 
> void foo(in C c) { c.m = 3; } // error
> 
> void foo(in C c) { c = new C(); } // ok
> 
> C foo(in C c) { return c; } // ok
> 
> void foo(in C c) { bar(c); } // ok
> 
> void foo(in C c)
> {   C d = c;
>     d.m = 3;  // ok
> }
> 
> What it provides is the most asked for characteristic of const.

Er, that's great, but does it provide for const return-values? You see, 
the above can be handled adequately via either struct or class /right 
now/ ~ but their immutablity can quickly unravel when, for example, 
exposing content via toString() or whatever. And please don't suggest 
returning a .dup of the content instead.




More information about the Digitalmars-d mailing list