Const Ideas

Jesse Phillips jessekphillips at gmail.com
Sat Dec 1 09:48:49 PST 2007


On Sat, 01 Dec 2007 06:35:46 +0000, Janice Caron wrote:

> On 11/30/07, Jason House <jason.james.house at gmail.com> wrote:
>> Craig Black wrote:
>> Technically, I proposed the syntax.  Janice didn't like it at first but
>> has now come full circle.
> 
> That's right. I think my earlier attempt was something like
> "const(X)ref" or "ref const(X)". I came full circle and now fully
> support Craig's idea, largely because I think there may come a time in
> the future when D has C++-style references. If and when that day comes,
> "&" will be preferable to "ref" for the same reason that "*" is
> preferable to "ptr". If it's /only/ classes we're talking about, it's no
> big deal either way, but if referencing gets more ubiquitious in the
> future, less typing => better.
> 
> So Craig gets the kudos for invention on this one. Nice one, Craig!

Based off of what you said in my other post, Walter will not change D's 
transitivity, Craig's proposal violates this. As a lot of the discussion 
here has suggested a solution for having a const ref with mutable data. 
So if I am correct then the real problem becomes with,

const(char)* foo == const(char*) foo
const(char)* foo != const char* foo
and thus
const X x != const(X) x

That is to say that const() means the reference can be rebound and const 
does not.

In this case I will re-propose Adam's idea to explain it that const() 
returns a constant of what is inside.

const TYPE * foo  // *foo immutable, foo mutable
const(TYPE) * foo // same as above
const(TYPE *) foo // foo and *foo immutable, but foo can be rebound
const(TYPE * foo) // nothing mutable, no rebinding of foo
TYPE * const foo  // Same as above

It would appear this meets at least most of the complaints const() does 
not have a special meaning, everything can be achieved with one const 
keyword, transitivity holds, no ambiguity, everything in const() is 
const. The only problem is that it would break all previous code, unless 
you assume const grabs everything after it so that the first and last are 
the same.



More information about the Digitalmars-d mailing list