Reuse of variables referencing const objects
Sergey Kovrov
kovrov+digitalmars at gmail.com
Fri Mar 6 03:55:03 PST 2009
I wonder if it is possible to reuse a variable for a const/invariant reference type? By "reuse" I mean to reference different data. Considering code:
auto f = new const(Foo)(); // Foo is a class, e.g. reference type
f = new Foo(); // error: cannot modify const
But here I really meant to assign new data to variable `f`, not to modify old instance.
Of course, I could use mutable pointer to const/invariant data. But this make sense only for value types. In case of reference types this will complicate things (introducing more human errors) and just feels wrong. Consider code:
const (Foo)* foo = a_condition ? &some_foo : null;
if (foo !is null && *foo !is null)
...;
Reference types was invented to simplify things. And they do, but not in case of const data. Do I miss something, or const support in reference types is not there yet?
-- serg.
More information about the Digitalmars-d-learn
mailing list