any news on const/invariant?

James Dennett jdennett at acm.org
Mon Dec 17 08:04:14 PST 2007


Jason House wrote:
> Bill Baxter Wrote:
> 
>> James Dennett wrote:
>>> Walter Bright wrote:
>>>> Many C++ people have argued
>>>> passionately for logical const, but I have become strongly convinced
>>>> that the notion is fatally flawed.
>>> How about a constant/immutable object that refers to and
>>> uses (but does not contain) a mutable object?  That's rather
>>> a common need, and it would be a shame not to be able to
>>> declare some object as being unchanging simply because it
>>> changes other objects' states, or to have to pass references
>>> to those other objects in to functions instead of holding
>>> them in member variables?
>> But the compiler needs to assume that every reference could be an owned 
>> reference since it doesn't know who you consider to be the "owner".
> 
> Is it really about ownership? 

More about "containment", but that implies ownership, and D
does not directly model containment as it uses reference
semantics for class types.

> That tends to imply to me that this would affect the garbage collector.

I don't think so.

> I think most posts on the topic look for exceptions to the transitive
> const rule.  i.e. where a pointer/class reference can point to some
> non-const data. 

No "looking for" such exceptions: merely pointing out that they
arise naturally in *designs* which take account of mutability,
and hoping for tools which allow us to express natural designs
to the compiler in a way that allows it to help to verify that
the code does what our design calls for.

> I don't fully appreciate the trouble having something like this
> would cause the compiler.  For parallelization of invariant
> objects, it seems about as bad as using a global variable.

Not so different.  There really are different forces at work
here: from an implementor's perspective, "transitive const" is
a much more convenient beast.  From a software designer's view,
it's a rather blunt tool compared to C++'s const.  It might or
might not be a pragmatic trade-off, but I think the battle for
a const which is as designer-friendly as C++'s has been lost
already.

-- James



More information about the Digitalmars-d mailing list