On 28-08-2011 01:05, bearophile wrote:
Mehrdad:
"With transitivity, there is no way to have a const pointer to mutable int."
But... so what? Maybe it should actually explain the benefit, since I
can't figure it out on my own.
D wants both mutable data and (strongly) pure functions. How do you bridge such
two worlds? If you have mutable data and you want to pass it to a pure
function, you need a way to say this data structure will not change, nor any
data it refers to. I think the D transitive const is the simpler way to do this.
C++'s non-transitivity seems to be quite type-safe, even if unintuitive
to the beginner (which I don't think it is). I *never* ran into issues
with it.
The C++ compiler doesn't offer means to enforce pure functions.
In my opinion it's possible to add a C++-style const to D too, but this further
increases the complexity of D, it cannot be used for purity, and it can't be
used for compiler optimizations, you are allowed to safely cast it away when
you want, it's just a convention, as in C++. But maybe it's useful anyway
because most times you try to mutate the first level of a data structure, and
such shallow const is enough to catch such unintentional mutation bug.
Bye,
bearophile
const_cast is the root of all evil. I really don't want to see it in D.
- Alex