Fully transitive const is not necessary

Steven Schveighoffer schveiguy at yahoo.com
Wed Apr 2 10:18:35 PDT 2008


"Janice Caron" wrote
> On 02/04/2008, Steven Schveighoffer wrote:
>> "Janice Caron" wrote
>>
>> > On 02/04/2008, Steven Schveighoffer wrote:
>>  >> "Janice Caron" wrote
>>  >>  > pure requires transitive const.
>>  >>
>>  >>  Forgive me if I don't take your word for it.  Please prove this.
>>  >>  Specifically, why pure fails with logical const (not head const as 
>> C++
>>  >> is).
>
>> I'm assuming you meant f to be part of Stupid?
>
> No, that was just a typo. Or lazy typing - take your pick. I should have 
> written
>
>
>    not_pure void f(const(Stupid) stupid)
>    {
>        int t = stupid.y;
>        ++stupid.t;
>        stupid.y = t;
>    }
>

Again, this is not a pure function.  You cannot access the mutable portion 
of a logically invariant data value.  This does not disprove that logical 
const can be used alongside pure.

>
>>  - a pure method cannot access the mutable portion of a logically 
>> invariant
>>  data value.
>
> Well, the easy way to do that is to redesign the class into two
> classes, one const and the other not. That way, you get the exact same
> benefit, but in addition, you end up saying what you mean, instead of
> writing obfuscated code.

I can ALREADY write "obfuscated" code as you say it.  See my original 
example.  Supporting logical const directly just makes that easier so I 
don't have to keep the part of the class that's not part of the state 
outside the class, and have to "work around" the const system.  Note that 
none of this violates const or functional programming.

The problem with your solution is that I can't just turn part of a class 
const, how would one specify that anyways?  I want to just say:

class C{ ??? int mutablePart; int constPart}

C c = new C;
const(C) cc = c; // mutablePart is still mutable

I don't even know how you can do something like this without templates, and 
then, I am bloating the namespace, and I can't automatically assign one to 
the other.

-Steve 





More information about the Digitalmars-d mailing list