const by default.

Walter Bright newshound at digitalmars.com
Fri Jul 7 10:42:41 PDT 2006


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.



More information about the Digitalmars-d mailing list