Const Ideas (and reference types)

Bill Baxter dnewsgroup at billbaxter.com
Fri Nov 30 12:44:33 PST 2007


Janice Caron wrote:
> Well, I'm still gunning for
> 
>     const (X)& x;
> 
> for mutable refs to const classes. Since I come from a C++ background,
> & means "reference of" to me, and this reads straightforwardly as "x
> is a reference to const X".
> 
> Of course, x would be a reference even /without/ the ampersand - such
> is the nature of classes in D. But writing it explicitly allows one to
> put it outside the brackets.

I like where this is going, but my guess is that when/if Walter ever 
introduces reference types, the syntax for reference-to-T will be "ref 
T" like the parameter signature rather than C++'s  "T&".

So if you're going peel off the hidden ref, I think you might should 
make it:

     ref X x --> const ref(T) x;
or          --> const (T)ref x;
or          --> ref const(T) x;

#3 seems pretty good to me.  But anyway no one knows what Walter will 
decide to do.

About references generally, one big difference between C++ references 
and D classes is that you can't reassign a C++ reference.
      // C++
      int& x = y;
      x = z;  /// error

In that sense, the C++ references themselves are always 'const' (aka 
head-const / final).
I seem to remember some example in Stroustrup that explained why this 
behavior was important.  But anyway, D's class references aren't like 
that, and it makes me wonder if more general D reference types in D 
would be like that.

--bb



More information about the Digitalmars-d mailing list