What all of these use-cases have in common is the fact that the state is private<br><br>Suppose that all class and struct members which were declared private, were always mutable, even if the class instance is const.<br><br>
That would mean you could do this:<br><br> class RandomNumberGenerator;<br> {<br> private long seed;<br> <br> const int rand() /* guarantees not to modify any non-private member variables */<br> {<br> seed = f(seed);
<br> return (seed >> 32);<br> }<br> }<br><br> const RandomNumberGenerator rng;<br> writefln(rng.rand());<br><br>In C++, we would have declared seed "private mutable" to achieve the same thing, but in D we tend to assume that everything in the same module is "friendly", so why not just let "private" mean "private mutable"?
<br><br><br>