mutable constant?
Jonathan M Davis
jmdavisProg at gmx.com
Wed Jun 26 17:53:30 PDT 2013
On Thursday, June 27, 2013 01:45:22 anonymous wrote:
> On Wednesday, 26 June 2013 at 15:48:42 UTC, Jonathan M Davis
>
> wrote:
> > It doesn't break anything. It just shows the need for pure.
>
> Really? In the following simplified code I see mutation of an
> immutable variable, which should not be possible, of course. That
> is breaking the type system, no? What am I missing?
>
> import std.stdio;
> int* point;
> struct TplPoint {
> int _point;
> this(int x) {
> _point = x;
> point = &_point;
> }
> }
> void main() {
> immutable TplPoint my = TplPoint(42);
> writeln(my._point); // 42
> *point = 13; // uh-oh
> writeln(my._point); // 13 !!!
> }
It looks to me like your code is fundamentally different from the OP's example
rather than being a simplification of the original code. In the OP's example,
the variable being mutated is a module-level variable, so the immutability of
the object is irrelevant when its member function is called (since it's not
the object itself which is being mutated). It also has nothing to do with
construction.
Your example, on the other hand, is showing a bug with regards to constructing
immutable objects in that the object doesn't actually become immutable until
it's fully constructed, and the compiler isn't catching something which then
violates the impending immutability. I _think_ that that bug has already been
reported, but I can't find it at the moment. But regardless, your example is
quite different from the OP's example.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list