What purpose to const besides functional programming?

Jason House jason.james.house at gmail.com
Wed Jul 23 18:17:25 PDT 2008


Walter Bright wrote:

> Jason House wrote:
>> Walter Bright Wrote:
>>> 5. Invariant data 
>>> does not have to be synchronized to be multithread accessible. 6.
>>> Invariant data improves the ability of automated tools to make
>>> inferences about what is happening. Optimization is an example of
>>> such.
>> 
>> #5 is not true.  Invariant functions require synchronization because
>> they can manipulate global state.
> 
> Not so. The only invariant functions in D are member functions which
> cannot change the state referenced by 'this'. They can certainly change
> other state. Pure functions, on the other hand, may not manipulate
> global state.

Maybe an example will help:

int a;
int b;

struct bar{
  void write(int x) invariant
  out{ assert(a==b); }
  body{
    a = x;
    b = x;
  }
}

The state of bar is never modified, but bar should not be used without
synchronization.  The function's contract can be violated due to a race
condition.  This may be an artificial example, but I hope it shows that #5
requires qualification.



More information about the Digitalmars-d mailing list