Why can't we define re-assignable const reference variable?

Yigal Chripun yigal100 at gmail.com
Wed Feb 20 15:03:46 PST 2008


Janice Caron wrote:
> On 20/02/2008, Yigal Chripun <yigal100 at gmail.com> wrote:
>   
>>  bound const Class c4 = new Class(); // bound (ie const) ref to const data
>>     
>
> Wouldn't the word "const" be superfluous in that example, since
> constancy is transitive? (If the reference is constant, the data
> /must/ be).
>
> It's an interesting idea, but I think, in general, if the compiler
> sees an array like
>
>     const(T)[] x;
>
> then it has to mean that the array's contents are not modifiable. That
> rule shouldn't depend on T, it should simply be true always. Ditto all
> other collections.
>   

As I've already said: it's only a start of an idea and should be further
refined.
that said, you are right that it's superfluous in the example above.
I've mainly suggested a new keyword and the idea to seperate the two use
cases of const.
I'm not sure if the "bound" property should be transitive or not ( i
suggested that it is but all options should be explored..) and it all
depends on what semantics you want to achieve. maybe (probably) bound
should also imply const on the referred data and const would imply all
refs inside the data object to be bound.

another tweak that can be done: intead of an error, if you apply bound
to a value type (like a primitive) the compiler implicitly "casts" that
to a const, that way you example above of an array would work for any
type T. it could be used the same way in generic programming.

--Yigal



More information about the Digitalmars-d mailing list