tail const
Fawzi Mohamed
fawzi at gmx.ch
Tue Nov 30 14:57:04 PST 2010
Speaking about D mistakes Steve spoke about missing tail const.
I was thinking about this, and I fully agree that it is a hole.
I don't know if it was already discussed, but I was thinking that one
could introduce
*const T t1;
and
*immutable T t2;
with the following meaning:
const or immutable is applied on the "dereferenced" type. For classes
this would mean the object, not the pointer.
Thus for example if T is a class type one would be able to reassign t1
t1=t2;
but not to modify the content of t1 or t2 in any way.
One can also extend it to array types: if T is U[], then it would mean
const(U)[] or immutable(U)[], and to pointer types,
*const int* would then mean const(int)*.
For other types maybe the best solution would be to
drop the const/immutable for basic types, functions and delegates
apply it to all fields of a struct (not sure how much work this would
be to implement)
This use of * should not introduce much ambiguity (a pointer is T*,
indeed also const*T would be almost as unambiguos).
One can see that this tail const is really a common type, indeed
string is such a type, and a function can be pure even if its
arguments is *immutable, because any changes would be done to a local
copy in the function.
I think that these things point toward the usefulness of a *const and
*immutable attributes.
Fawzi
More information about the Digitalmars-d
mailing list