Head Const
Jonathan M Davis via Digitalmars-d
digitalmars-d at puremagic.com
Wed Feb 17 09:47:02 PST 2016
On Wednesday, 17 February 2016 at 17:25:40 UTC, rsw0x wrote:
> It's weird because usually D prefers the practical solution
> over the ivory tower one. Nearly every time I end up using
> immutable or const for anything beyond say, a trivial function
> parameter, I always end up removing it.
> My C++ code is often far, far more 'const correct' than my D
> code.
Well, Walter's take on it is that because C++'s const doesn't
actually guarantee anything, it really isn't much different from
simply documenting your code, and it's essentially useless. And
to a point, he's right. All C++'s const does is prevent
accidental mutation and serve as documentation that the code
isn't supposed to mutate the logical state of the object. It's
trivially circumvented to the point that it doesn't actually
guarantee anything and can't actually be relied on for anything.
So, essentially the argument is that C++'s const isn't actually
practical. It's just fooling you into thinking that you're
getting guarantees that you're not actually getting.
But in practice, it does prevent accidental mutation, and as long
as the programmer is behaving themselves reasonably well, it
functions as documentation. And IMHO that definitely is worth
something, much as Walter thinks that it isn't. So, while C++'s
const doesn't really guarantee much, I'd much rather have it than
nothing.
A lot of what it comes down to though from a purely practical
standpoint is that because we have immutable, const naturally
becomes more restrictive, and while immutable obviously can't be
used in many cases, it _does_ allow for cleaner, more efficient
code where it can be used. So, as soon as you have immutable,
you're almost forced to go to where D is with const - at least as
long as const can refer to something that's immutable.
Unfortunately, the net result is that while const is still very
useful, there are a lot of cases in D where you can't use it, and
you have to be a lot more careful about how and when you use it
such that it gets used far less than in C++, and while where it
_is_ used does provide much better guarantees, you lose out on a
lot of protection against accidental mutation that your typical
C++ code gets.
Personally, I'm quite divided on the matter. The extra guarantees
of D's const are very nice to have, but since it doesn't have any
backdoors, there are a number of idioms that simply can't be done
in D as long as const is involved, which definitely sucks. But
interestingly, the more I've used D, the less happy I've been
with C++'s const. It just has too many holes. In particular, the
fact that it's not transitive is incredibly annoying, making it
trivial to have cases where you give mutable access to something
where you intended for it to be const, but it was only partially
const. Even if we were to do something like add @mutable to D, I
never want to see non-transitive const in D, and I wish that
C++'s const were transitive, even if all of the rest of it was
the same.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list