Inherited const when you need to mutate

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Jul 10 17:31:27 PDT 2012


On Tue, Jul 10, 2012 at 04:58:06PM -0700, Walter Bright wrote:
> On 7/10/2012 4:19 PM, H. S. Teoh wrote:
> >On Tue, Jul 10, 2012 at 04:05:51PM -0700, Walter Bright wrote:
> >>On 7/10/2012 4:05 PM, H. S. Teoh wrote:
[...]
> >Nqt in this case. The const(B) reference does not permit any of B's
> >methods to mutate the members of B -- you cannot downcast a const(B)
> >reference to a C reference. As far as the methods of B are concerned,
> >the object is immutable.
> >
> >The interesting part is when C's methods override B's methods: those
> >methods _can_ mutate the object, but not the members inherited from
> >B.  And this does not break immutability; you cannot cast const(B) to
> >C, so if you start with immutable(B), it will remain immutable no
> >matter what.  You can't call C's methods from the const(B) reference,
> >and you can't override B's methods with C's mutating methods without
> >actually having a mutable C to begin with.
> >
> >
> >T
> >
> 
> If you've found a way to mutate const and have it stay const, then
> there's a hole in the typing system.

The const in const(B) applies to the B portion of the object. Just
because the C portion changes, doesn't violate the const-ness of the B
portion.

I know this isn't how the current type system works, but perhaps it can
be made to work in a way that makes sense.


T

-- 
Real Programmers use "cat > a.out".


More information about the Digitalmars-d mailing list