What purpose to const besides functional programming?

Steven Schveighoffer schveiguy at yahoo.com
Thu Jul 24 07:30:13 PDT 2008


"Jason House" wrote
> Simen Kjaeraas Wrote:
>> You are expecting invariant member functions to not do things they can
>> and should do.
>
> My example was in response to Walter's statement (#5) that invariant 
> objects can be accessed without synchronization.  I was providing a simple 
> counter example.

Your counter-example fails to show that.  You can access invariant objects 
without synchronization, you cannot necessarily call invariant functions 
without synchronization.  In your example, you are accessing a and b, which 
are not invariant, which means you will have to synchronize on something. 
If there was data you were accessing in the struct itself, then you would 
not need to synchronize to access that data.  If you are accessing both 
struct data and global data, you only need to synchronize on the global data 
access.  And you will need to synchronize on something outside the struct, 
as other classes might be able to access global data.

Let me show you exactly how the compiler views your code:

int a;
int b;

struct bar{
}

void bar.write(invariant bar *this, int x)
  out{ assert(a==b); }
  body{
    a = x;
    b = x;
  }

-Steve 





More information about the Digitalmars-d mailing list