Do we really need const?

Christopher Wright dhasenan at gmail.com
Mon Sep 17 10:54:08 PDT 2007


Jarrett Billingsley wrote:
> "Bill Baxter" <dnewsgroup at billbaxter.com> wrote in message 
> news:fcl79k$18vu$1 at digitalmars.com...
> 
>> It seems from the discussion here the past week, there is no real 
>> multithreading benefit to be had from const/invariant.  'Pure' is where 
>> it's at for that.  So maybe we're just better off without the complexities 
>> of const.  I've certainly gotten used to the lack of const in Python, so 
>> why not in a C++-ish language?
> 
> I start to wonder how much of the "of COURSE we need const!" thinking comes 
> from "if all you have is a hammer, everything starts looking like a nail." 
> That is, I wonder how many people think we absolutely need const because 
> they're so used to using it in C and C++.

I think it would be a comfort to have it for function signatures, but 
I've only been affected by it once -- with getopt on C. (And getopt is 
an ugly, horrible thing. _Why_ does it modify the command line arguments?)

Other than that, D2 invariant and final seem useful. D2 const is good 
for basic things, but seems to scale poorly.

> Never having learned a const-correct language (started with various BASICs, 
> then on to very rudimentary C++, then D, and also a bit of Java and C#), I 
> simply don't see the overwhelming need.  Sure, there are a very few cases 
> where const could be useful in my code for efficiency's sake, but I've never 
> felt like I was missing any expressive power.  Furthermore, I have been 
> bitten exactly once by something that theoretically could have been 
> prevented by const.  It took me two minutes in the debugger to track down 
> the problem.  I'd say that those two minutes were a much better tradeoff 
> than the god-knows-how-long it'd take to write const and non-const overloads 
> of who-knows-how-many functions. 

Most of the time, you shouldn't need to write those overloads. Either 
the function can be const, in which case you can use it with mutable 
data anyway, or it can't, in which case you can't write a const overload 
for it. At least in an ideal system.



More information about the Digitalmars-d mailing list