I just got it! (invariant/const)

Steven Schveighoffer schveiguy at yahoo.com
Wed Apr 9 10:41:18 PDT 2008


"guslay" wrote
> Steven Schveighoffer Wrote:
>
>> "guslay" wrote
>> > Janice Caron Wrote:
>> >
>> >> On 09/04/2008, Georg Wrede wrote:
>> >>
>> >>     int f(invariant D d) invariant pure { ... }
>> >
>> > Shouldn't it be
>> >
>> > int f(invariant D d) pure { ... }
>> >
>> > Pure functions are a subset of invariant functions, no?
>>
>> No.
>>
>> A function does not necessarily need to take an invariant 'this' pointer 
>> to
>> be pure.
>>
>> For example:
>>
>> class C
>> {
>>    invariant int x;
>>    pure int getX() { return x;}
>> }
>>
>> void foo()
>> {
>> C c = new C;
>> c.getX(); // ok
>> }
>>
>> -Steve
>
> c.getX() is equivalent to " int getX( ref C this ) ", so its not pure.

AFAIK, C.getX() is equivalent to int getX(C this).

i.e. you can't change the this pointer to point to something else during the 
function.

I don't see why it's not pure.  I'm questioning the assertion that pure 
functions MUST take invariant reference types.  They can take mutable ones 
as long as they do not reference mutable data within the type.  It's the 
same reason you will be able to pass strings to a pure function.  A string 
is mutable, but the data it points to is not.

-Steve 





More information about the Digitalmars-d mailing list