Extended Type Design.
kris
foo at bar.com
Thu Mar 15 23:45:05 PDT 2007
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
>
>
>
> Andrei
... and return values: "const char[] immutableTextProp()" ?
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?
More information about the Digitalmars-d
mailing list