Optimizing Immutable and Purity

Jason House jason.james.house at gmail.com
Wed Dec 24 05:37:10 PST 2008


Andrei Alexandrescu Wrote:

> Walter Bright wrote:
> > Andrei Alexandrescu wrote: 
> >> Const is still useful because inside a function you know for sure that 
> >> another thread can't modify the data.
> > 
> > I think you meant immutable.
> 
> I meant const.
> 
> Andrei

At least for the current D2, your statement is very wrong. For function calls, const is merely a promise by the callee to not modify the data or call non-const member functions. Any data can be passed into the function. That means that mutable references can exist through other threads, global variables, or through other input parameters. Const data can change at any time.

Even if we jump forward to a D2 where only shared const can be modified by other threads, the guarantees are not much better. Every impure function call can modify the data. That includes const member functions of const data! I've complained about that last bit many times.

The bottom line is that const gives no guarantees.



More information about the Digitalmars-d mailing list