Okay, what happened to my literal strings?
Walter Bright
newshound1 at digitalmars.com
Thu Sep 6 16:44:44 PDT 2007
Burton Radons wrote:
> Ah, so 'const char [] foo = "bar";' works, does it? No thank you. Const
> forcing (whatever the name given) is the kind of tiddlywinks
> obsessive-compulsive bullshit that can't even be enforced so it's
> useless to everybody* which caused me to flee from C++.
I hear you, Burton, and I resisted const for a long time for exactly
those reasons. But there are some compelling reasons to support const:
1) It opens the door to functional programming. This means that function
calls can be automatically parallelized, which is going to become an
increasingly big deal as people try to figure out how to use their
multicore processors.
Note that C++ const CANNOT do FP, and Java CANNOT do const. This will
open up a huge advantage for D.
2) People who work on large projects with several teams modifying the
code tell me unequivocably that they need some sort of const to enforce
modularity.
3) Const improves the self-documentation of interfaces.
As for strings being invariant, experience across many languages
demonstrates that invariant strings are the most natural and productive
way for people to think about them.
I went through Phobos replacing the char[]'s with string's, and I found
out a couple of things:
1) about 99% of the char[]'s were immutable anyway, and fit right in
with being string's
2) the edits can be done mechanically, just s/char[]/string/ across the
files, compile, and revert the very small number of ones that are mutable.
For other data types, you can often just ignore const and not use it.
I want to reiterate that C++ const fails at (1), (2), and (3), because
C++ const is not transitive and is not enforced. Furthermore, the
shortsighted "mutable" keyword just pulls the rug out from under generic
programming with const.
More information about the Digitalmars-d
mailing list