Fully transitive const is not necessary
Janice Caron
caron800 at googlemail.com
Wed Apr 2 11:15:39 PDT 2008
On 02/04/2008, Steven Schveighoffer <schveiguy at yahoo.com> wrote:
> globby classes are equivalent to muty classes because in both cases I am
> storing information outside the const scope. In globby classes, I'm storing
> it in a global AA that has an element for each instance of the class that's
> created. In muty classes, I'm storing it in a member variable with the
> class. In all cases, it's not considered to be a state of the class, it's
> just that for muty classes, I'm storing it with the class (and reaping the
> benefits of not having to manage a globally stored AA).
>
> I can prove that muty classes are equivalent to globby classes:
>
> class muty
> {
> mutable X x;
> }
>
> is equivalent to
> class globby
> {
> X x() const {...}
> X x(X newvalue) const {...}
> }
So you're mimicking mutable fields with a global AA. Gotcha.
There are several problems with this, including:
(1) the global variable is visible to at least the entire module, so
something else might modify it when you're not looking.
(2) are we really sure that modifying an AA is an atomic operation? I'm not.
It's also worth mentioning that you won't be able to modify global
variables in a pure function, so the ability to mimic mutable member
variables is lost at that point.
More information about the Digitalmars-d
mailing list