Extended Type Design.

Derek Parnell derek at nomail.afraid.org
Fri Mar 16 00:10:57 PDT 2007


On Thu, 15 Mar 2007 23:45:05 -0700, kris wrote:

> Andrei Alexandrescu (See Website For Email) wrote:
>> 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

I'm not sure about that last one. Does this mean that p2 points to an int
that cannot change and p2 itself can no longer be changed? And what is the
value in the int that p2 points to?

Can you do this sort of thing ...

 int x = 5;
 const! int* p2 = new const! int(x);

meaning that p2 now points to a 'final' int whose value is 5, plus p2 can
never be changed now either.


> ... and return values: "const char[] immutableTextProp()" ?

Hmmmm... does "const char[] X" mean that X.ptr can change, X.length can
never change and the data pointed to by X.ptr can never change? Or is "can
never" too strong? Is it more that code within the scope of the declaration
is not allowed to change those things?

> Just a thought on the const! syntax; it looks somewhat odd, and perhaps 
> too much like template syntax?  How about calling one of the "const" 
> types "readonly" or "view" instead?

Yes! "const!" is bad form. Really bad. Horrible in fact. Just make it a new
keyword please, without using non-alpha characters. Such as "readonly",
"immutable", "keepyourgrubbyhandsoff", ... anything but using "!".

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Justice for David Hicks!"
16/03/2007 6:01:40 PM



More information about the Digitalmars-d mailing list