What purpose to const besides functional programming?
Walter Bright
newshound1 at digitalmars.com
Thu Jul 24 16:20:01 PDT 2008
Jason House wrote:
>>> #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.
You're imputing meaning to an "invariant function" that isn't there. An
invariant function is a function that does not change the state of the
object it is a member of. It can change other state.
More information about the Digitalmars-d
mailing list