Suggestion: object const'ness

ChristopheBourez ChristopheBourez_member at pathlink.com
Sun May 21 07:46:25 PDT 2006


In article <e4ojvc$ppj$1 at digitaldaemon.com>, Jarrett Billingsley says...
>
>"ChristopheBourez" <ChristopheBourez_member at pathlink.com> wrote in message 
>news:e4kik9$1ntj$1 at digitaldaemon.com...
>
>> It should be interesting to have a const'ness ala C++, I mean, the ability 
>> to
>> declare methods that do not modify the state of objects.
>> Any comments?
>
>I think it's all just a convoluted mess.  Considering how many levels of 
>const-ness you can have (can I modify the reference?  or the contents 
>thereof?  how about arrays?  can I change their contents, or just where they 
>point?), and the obvious problem that you can cast it away (without the need 
>for ASM, something Tom mentioned), I don't see how it could be implemented 
>orthogonally while still being useful.  Const also means different things to 
>different people, so there'd be a lot of dissention over exactly what it 
>should do.
>
>Having taught myself how to program C++ from a relatively old book, I never 
>really learned to write "const-correct" code.  Even so, having written some 
>C++, and a lot more in D, I've _never_ run into a bug caused by my 
>modification of something that I wasn't supposed to be modifying.  I'm sure 
>some people have, but the concept of "const" seems like an overkill solution 
>for it.
>
>I really don't want to see D code looking like some C++ code, where const is 
>applied to absolutely _everything_:
>
>const int[] const func(const float x, const char[] const str) const;
>
>Wheeeeeee. 
>
>

I was just mentionning the possibility to define methods that do not modify the
internal state of an object, in order to prevent the implementer to modify
inadvertently its object. If it should be the case, the implementer should be
faced to a contradiction: solving this contradiction does not mean necessary to
drop the const'ness of the method. This may also mean a design error. You are
arguing that you never learned to write const-correct code. You are arguing that
you _never_ had bugs... as long as you develop and maintain your own code
without sharing it with another people. For my part, I will argue to you the
exact opposite. I develop in C++ for 12 years, applying the const'ness all the
time. I do not say to redo what C++ does. I do not avan state that C++ provides
an ideal solution for the const'ness concept. It is not a reason for purely and
simply ignoring this concept.

Christophe



More information about the Digitalmars-d mailing list