tail const

Jonathan M Davis jmdavisProg at gmx.com
Wed Dec 1 03:17:24 PST 2010


On Wednesday 01 December 2010 02:56:58 so wrote:
> > 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;
> 
> Sorry if i am overlooking something but if we are going that far, why not
> just :
> 
> const(int)* p; // tail const pointer   - already here
> const(int)& r; // tail const reference - will be introduced and quite
> straightforward.
> 
> > 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.
> 
> It is indeed common and IMHO it is the biggest reason why pointers are
> still used too much in C++ where references should be the obvious choice.

Various syntaxes have been proposed in the past. Syntax isn't really the issue. 
It's pretty easy to come up with one. I think that out of the ones I've seen, 
the I liked the best was the one proposed by Michel Fortin:

>I proposed the following a while ago. First allow the class reference
> 
> to (optionally) be made explicit:
>         C a;     // mutable reference to mutable class
>         C ref b; // mutable reference to mutable class
> 
> And now you can apply tail-const to it:
>         const(C)ref c;  // mutable reference to const class
>         const(C ref) d; // const reference to const class
>         const(C) e;     // const reference to const class

The real issue is not syntax but getting it into the compiler. Apparently, there 
are difficulties in implementing tail const in the compiler which made Walter give 
up on it in the past. It should be doable, but Walter is totally sick of the 
issue and doesn't want to put the time in to do it - he has plenty on his plate 
as it is. So, if it's going to be done, someone else has to step up to the plate 
and do it. And with the general lack of dmd developers, that hasn't happened. No 
one thus far has had both the inclination and the time.

- Jonathan M Davis


More information about the Digitalmars-d mailing list