Associative Array of Const Objects?

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Mar 29 12:58:28 PDT 2015


On Sunday, 29 March 2015 at 19:13:32 UTC, bitwise wrote:
> Interesting, but I still don't understand why D doesn't have 
> something like this:
>
> const Test test;    // or const(Test) test;
> test = new Test()  // fine, underlaying data is const, the 
> reference is not
>
> Test const test = new Test();
> test.a = 5;              // fine, test is read-only but 
> underlaying data is not const
> test = new Test();  // error: test is read-only
>
> const(Test) const test = new Test();
> test.a = 5;              // error, underlaying data is const
> test = new Test();  // error: read-only

I think the semantics you propose here are not good. The first 
example would change the meaning of existing syntax (bad). The 
second one shows a const reference to mutable data (head const), 
which is a no-go for D so far.

Shuffling things around, this could be less disruptive addition 
to the language:

Test const test; /* mutable reference to const data */

But:

1) Such placement based syntax is foreign to D.

2) It would be special syntax just for class types.

3) It's not how C++ rolls.
`const Test test;` and `Test const test;` are equivalent in C++. 
You need that '*' in C++, too, to make a distinction between 
reference and data.

4) Rebindable works reasonably well, as far as I know. So there 
is not that much pressure to change the language.


More information about the Digitalmars-d-learn mailing list