Extended Type Design.

Andrei Alexandrescu (See Website For Email) SeeWebsiteForEmail at erdani.org
Thu Mar 15 23:21:26 PDT 2007


Daniel Keep wrote:
> This is fantastic news.  I'm really happy to know you guys are making
> progress on this.

Thank you! In fact, it did take an embarrassingly long time to implement 
something within all of the self-imposed constraints, but now we're 
really happy with the result.

> So if I understand this right:
> 
> * "final int*" is an immutable pointer that points to mutable data,

Yes. For example:

int a, b;
final int * p = &a;
*p = 5; // fine
p = &b; // error! Cannot rebind final symbol!

> * "const int*" is a mutable pointer to immutable data, but that data
> *may* be mutable in another context (ie: const((new int[10]).ptr)) and

Exactly. For example:

int a;
const int * p = &a;
*p = 5; // error! Cannot modify const data!
a = 5; // yah, modify data under the feet of p

This is a great feature for calling functions and providing "isolated" 
interfaces. For example, say you write a function

void print(char[]);

You want to clarify to the caller that you NEVER modify their data. So 
you define the function as:

void print(const char[]);

Then callers can call print with mutable or immutable data; it makes no 
difference to print.

Unlike in C++, it takes much more effort to cast away constness, and the 
results are never well-defined.

> * "const! int*" means "seriously, this *will not* change".

Right. For example:

int a;
const! int* p = &a; // error! cannot convert int* to const! int*
final int b = 42;
const! int* p1 = &b; // yah, b is truly immutable
const! int* p2 = new const! int; // yah



Andrei



More information about the Digitalmars-d mailing list