Fully transitive const is not necessary

Steven Schveighoffer schveiguy at yahoo.com
Tue Apr 1 08:08:33 PDT 2008


"Janice Caron" wrote
> On 01/04/2008, Steven Schveighoffer wrote:
>>  Second, I'll show how logical const is ALREADY available with D's const,
>>  just in a less convenient form.  Let's look at an invariant property 
>> that is
>>  not invariant:
>>
>>  class X
>>  {
>>    private static int _x;
>>    invariant int x()
>>    {
>>        return _x++;
>>    }
>>  }
>
> That does not break transitivity. You have misunderstood.
>
> "invariant int x()" declares x() to be a function, within whose body
> the variable "this" is invariant. X._x is a global variable, unrelated
> to "this". Transitivity is not compromised.

No you have missed my point :)  Yes, transitivity is still intact, but 
Walter is always saying that fully transitive const is necessary for 
compiler optimizations, and so there is no place for logical const.  My 
point is, even with fully transitive const, the compiler cannot do 
optimizations based on the fact that a function is invariant, as it cannot 
guarantee that the function has no side effects.  And in fact, this IS 
logically const.  Logical const does not break transitive const, and despite 
all efforts to make logical const invalid, it is still present.

>
>
>>  Notice that despite all efforts of the compiler to make sure invariant 
>> is
>>  transitive, it still cannot do any parallelizations or optimizations on 
>> the
>>  call to x.  If it does not have the source code for x, it cannot be sure
>>  that x isn't doing something like the above.
>
> However, in the future, you will be able to declare
>
>    class X
>    {
>        private static int _x;
>        invariant int x()
>        {
>            return _x++; /*ERROR*/
>       }
>   }
>
> and suddenly - bingo - the offending line will not compile.
>
> You gotta think ahead.

I should hope this is not the case.  My understanding is that invariant and 
const will remain intact, and pure functions will be introduced to allow 
this kind of behavior.  What new rule are you thinking of that will apply to 
an invariant function that forces this?  Remember than an invariant function 
just means the instance is invariant, not the static or global data.

My entire point is that pure functions can exist with logical const, so 
there is no reason (that I know of) we need fully transitive const.  When 
pure functions are introduced, they simply will not be allowed to use the 
mutable members of a logically const class/struct.

-Steve 





More information about the Digitalmars-d mailing list