Const Ideas

Craig Black cblack at ara.com
Fri Nov 30 09:38:32 PST 2007


The 2.008 release is an improvement, but it seems like there is still a lot 
of dissatisfaction with the current const stuff.  Janice proposed an idea 
for constifying instance member functions using const(this).  I like that 
idea.  It is explicit and can be used as a prefix without ambiguity.  If we 
did this, I would suggest getting rid of the postfix syntax.

const(this):
void func1() { ... }
void func2() { ... }
void func3() { ... }

or

const(this) void func1() { ... }
const(this) void func2() { ... }
const(this) void func3() { ... }

This is both explicit and remedies the ambiguity between the instance 
pointer and the return type being const.

Another point that Janice makes is that it is confusing that const(X) isn't 
the same as const X.  To solve this problem, I propose using const(ref) to 
constify references.  This removes ambiguity and makes things explicit.

const X x;  // The data is const, the reference is mutable
const(ref) X x;  // The data is mutable, the reference is const
const(ref) const X x;  // Everything is const, nothing is mutable

-Craig 





More information about the Digitalmars-d mailing list