Poll: Would you like to try const-by-default or not?

Bill Baxter dnewsgroup at billbaxter.com
Sun Jun 10 11:56:25 PDT 2007


Jarrett Billingsley wrote:
> This is not a discussion thread.  I think we've discussed enough.  :)
> 
> Basically there are these possible const implementations (there are others, 
> but for the sake of simplicitly, I'll keep this poll to 2):
> 
> 1) C++ style const, where you mark anything that should be const as such.
> 
> 2) Parameters are const-by-default, and must be marked mutable otherwise. 
> Locals, fields etc. are still mutable by default.
> 
> Walter doesn't want to stray from option 1, because 2 is basically "too 
> weird."
> 

I would really like to give option 2 a try too.  I mean we have very 
concrete and oft-encountered reasons why #2 should be better on the one 
hand
  * better safety / documentation
    (because compiler won't let you forget a necessary 'mutable')
  * better maintainablity
    (because fewer users of an API will be affected by later changing a 
parameter to 'mutable' than to 'const')
  * less typing
    (since more paramters are const than not)

and a lot of vague hand-waving on the other
* "just too different"
    (but apparently it's ok for perl6 or euphoria)
* "It'll be weird having extern C functions act differently from D ones"
   (they already do)
* "having same type defaults is better for generics"
   (maybe...but I'm not convinced. If you have powerful enough 
metaprogramming it shouldn't be hard to strip const from a type tuple, 
or add it.  And people do far more programming than meta-programming.)


If someone could A) come up with realistic concrete examples of #2 being 
problematic or a severe pain to use and B) also convince me that the 
problematic case is more important than gaining extra safety and peace 
of mind on every single function I write, then I'll give up on #2.  But 
no one has done that yet.  And I suspect the only way we'll find out is 
actually going through the exercise of converting code to work with 
const default.

So here's a suggestion/challenge.  We don't have to wait for Walter. 
Pick your favorite small, well contained C++ or D library and just try 
to rewrite it with const as default for parameters and use "inout" or 
"mutable" to tag modifiable arguments.  You may not get everything 
right, but if you take the time to reason through it like a "human 
compiler" it should be close enough to see what the change would be 
like.  In some professions they don't have the luxury of a magic program 
to tell them whether what they write is correct or not, so they do 
things the old fashioned way -- they use their brains. In fact that's 
probably the case in most every profession besides computer science.  We 
have it easy. :-)

--bb



More information about the Digitalmars-d mailing list