Logical const

bearophile bearophileHUGS at lycos.com
Sun Nov 28 16:29:30 PST 2010


Peter Alexander:

> If I give some const object to a function:
> 
> void render(const GameObject&);
> 
> GameObject obj;
> render(obj);
> 
> I can be sure that my object will come back unmodified.

render() is free to modify the objects contained inside GameObject, because that const isn't transitive.


> Yes, GameObject could be unreasonably mutilated by careless use of 
> mutable, but in practice that simply doesn't happen.

Likewise, in Python there is no const attribute, yet those program often don't have bugs.

The D transitive immutability is more rigid than the C++ const, it has a higher usage cost for the programmer, but it gives you back a stronger enforced semantics of immutability (strong enough for functional parallelism).

Bye,
bearophile


More information about the Digitalmars-d mailing list