Const Ideas

Jason House jason.james.house at gmail.com
Fri Nov 30 14:17:17 PST 2007


Craig Black wrote:

> 
> "Janice Caron" <caron800 at googlemail.com> wrote in message
> news:mailman.206.1196453000.2338.digitalmars-d at puremagic.com...
>> 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 now better understand why you proposed this syntax.  I guess it's not so
> bad.  I can't think of anything better.

Technically, I proposed the syntax.  Janice didn't like it at first but has
now come full circle.

I've had some second thoughts about stuff, but I'm still able to convince
myself that a novel syntax for rebindable constant objects is best.

Here's what I consider to be requirements:
  const(...) must mean that everything within the parens is constant
  "const X" and "const(X)" must mean the same thing
  Rebindable constant objects are important to have
  const(X) foo(){...} should mean that what it returns is constant.
  "non-rebindable const(X) x = foo();" should be allowed
  "rebindable const(X) x = foo();" should be allowed
  "const(X) foo(){non-rebindable const(X) x; return x;}" should be allowed
  "const(X) foo(){rebindable const(X) x; return x;}" should be allowed.

>From those requirements, "const(X) x;" must mean that x can not be rebound. 
Using "const(X)& x;" for a rebindable definition seems to be the best I can
come up with.

Do people disagree with any of the requirements I listed?  Most are just
sanity requirements, but may not have general agreement.  Beyond that, is
there a better for for a rebindable definition?



More information about the Digitalmars-d mailing list