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

Sergey Gromov snake.scaly at gmail.com
Wed Feb 20 13:32:23 PST 2008


Janice Caron <caron800 at googlemail.com> wrote:
> Well then you really haven't understood it all. The characters of a
> string may be stored in a ROM segment, and for that reason, the
> characters of a string may not be modified.

Of course I understand that.

> But there is no reason why
> you can't modify slices which merely point into that ROM data.

Modifying a slice which points i-dont-know-where feels unsafe to me.  I 
won't use this feature as long as I can.

> > Actually, const() syntax is already different from const attribute.
> 
> Again, you're wrong. The following two lines
> 
>     const int x;
>     const(int)x;
> 
> are identical in meaning. And that remains true for any type, no
> matter how complicated, e.g.

I'm talking about syntax, not semantics. const is an attribute, const() 
is a function.  I believe that the language lexer was specifically 
modified to allow for this functional const notation.

> >  Probably if we had a tailconst attribute instead, so that it was
> >  possible to write
> >
> >  tailconst
> >  {
> >         C c;
> >         int x;  // either illegal, or not affected
> >  }
> 
> That is something which no one has ever suggested before!
> 
> Oh, plenty of people have suggested tailconst, but it's generally
> expected to be more along the lines of
> 
>     tailconst(C) c;

This is strange.  tailconst attribute merges smoothly with the language 
design.  On the contrary, the functional attributes, const included, 
look strange.

> >  This should of
> >  course deprecate the use of non-attribute const() form.
> 
> You can't! How could you declare
> 
>     const(int[][][])[][][] x;
> 
> without the brackets? (Not that I have a use for that particular type,
> but hopefully you see the point).

This use of "partial const", in addition to "full const" and "tail 
const", is specific to multi-dimentional arrays.  No other types can 
make use of it.  With arrays, you probably can do with aliasing:

	alias const int[][][] constArr;
	constArr[][][] x;

-- 
SnakE



More information about the Digitalmars-d mailing list