Transitive const sucks

Alex Burton alexibu at mac.com
Tue Sep 11 04:42:26 PDT 2007


Regan Heath Wrote:


> I remember your example case from your earlier thread. :)
> 
> So, correct me if I'm wrong.  You want to be able to specify a reference 
> which itself cannot change (perhaps a requirement, perhaps not?), but 
> refers to an object which can.  And you want this reference to be usable 
> inside a const method.
> 
> For the first I would suggest a new syntax like:
> 
> class Foo { int a; }
> const(Foo&) pFoo;  //pFoo cannot change, pFoo.a can
> 
> This is analogous to my other idea[1].
> 
> For the second, either:
> 
> 1. the compiler should assume that as the author used const on this 
> reference, and intentionally applied it only to the reference itself, 
> that changes made _via_ this reference are not covered by the const 
> method contract.
> 
> 2. we need a new keyword to indicate which references/pointers to 
> mutable data can be used in a const method. i.e. "mutable"
> 
> in either case you would then be able to make changes to the referenced 
> data from within a const method.

Yes essentially we need to be able to distinguish between mutable references and non mutable references. The latter being logically part of the class.
Note I would not suggest a mutable keyword that could be applied to class members that are structs or ints, like you can use in c++, as this is clearly bad for optimisation and is logically wrong.
But what I do want is essentially an ability to mark class references as mutable.

On the one hand I totally empathise with being able to write 'Socket sock;' for members that are classes possibly polymorphic. Instead of in c++ aggregate_ptr<Socket> sock, or scoped_ptr<Socket> sock or shared_ptr<Socket> sock;
Most of the needs of having these different pointer types was to do with memory management, which has been eliminated in D.
But there was also information on aggregation (part of relationships) in there.

The mutableness is just a property of the member not being part of the class, so what we really need is something like:

class Whole
{
    Part p;
    not_a_part_keyword NotAPart np;
};



More information about the Digitalmars-d mailing list