On Sunday, 29 March 2015 at 19:04:30 UTC, anonymous wrote:
Notice how you have that '*' there that allows you to
distinguish the data from the reference.
You can have a mutable pointer to const data in D, too:
struct Test {}
const(Test)* test1 = null;
test1 = new Test; /* fine */
const(Test*) test2 = null;
/* equivalent variant: const Test* test2 = null; */
test2 = new Test; /* Error: cannot modify const expression
test2 */
You cannot have a mutable class object reference to const data,
because syntactically that distinction isn't made in D. There
is std.typecons.Rebindable, though:
import std.typecons: Rebindable;
class Test {}
Rebindable!(const Test) test = null;
test = new Test; /* fine */
I would have suggested that I got things backward, but this
doesn't work either:
const Test test = new Test();
test = new Test(); // error: cannot modify const expression
`const Test test` is the same as `const(Test) test`.
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