mutable constant?

anonymous anonymous at example.com
Wed Jun 26 18:31:01 PDT 2013


On Thursday, 27 June 2013 at 00:53:48 UTC, Jonathan M Davis wrote:
> 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 don't see the fundamental difference. In both versions:

- An immutable struct instance is constructed.
mine: immutable TplPoint my = TplPoint(42);
OP: const TplPoint!float my = TplPoint!float(42, 23);
     Note that this does not set my._point to Point(42, 23).

- The constructor stores a mutable pointer to its contents in 
module scope.
mine: point = &_point;
OP: points ~= &this._point;

- Via that mutable pointer the data is altered.
mine: *point = 13;
OP: a bit more convoluted, TplPoint.ptr does the nasty work, but 
it could just as well be in main: points[someid].x = somevalue;

=> Immutability broken.


More information about the Digitalmars-d-learn mailing list