Suggestion: object const'ness

Tom ihate at spam.com
Sat May 20 23:12:11 PDT 2006


Jarrett Billingsley escribió:
> "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?),

Implementation difficulty: VERY MUCH.

> and the obvious problem that you can cast it away (without the need 
> for ASM, something Tom mentioned),

That isn't a problem at all. If one do cast constness away one always do 
that VERY explicitly. So it's not an issue been allowed to do that 
meanwhile it's not easy to do it by mistake. Obviously, the C++ cast to 
do this isn't that simple to be written by mistake.

> I don't see how it could be implemented 
> orthogonally while still being useful.

It is useful. But I agree, it's difficult though if we could have just a 
subset of these constness capabilities it would be nice and 
not-that-hard to make it true.

For example, methods that aren't allowed to modify the object. This 
isn't a big deal I guess (to implement I mean).
True IN parameters aren't a big deal either.
The rest may stay below the magnifying glass for a while.

> Const also means different things to 
> different people, so there'd be a lot of dissention over exactly what it 
> should do.

I can't agree with you. With that kind of thought I can say that IN 
parameters mean a really different thing in D than what they mean to me. 
So why would we have to read documentation and learn the language? we 
can make a language that doesn't need training because all of its 
features are universally known from the cradle.

> Having taught myself how to program C++ from a relatively old book, I never 
> really learned to write "const-correct" code. 

It isn't really that difficult, believe me.

> 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.

Not at all, it is a very nice solution. I did see const helping a lot 
preventing bugs (in my experience).

> 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. 

That I agree. That's why we should have just a subset of C++ constness 
in D. I hate the constness been dragged along with the type everywhere.

--
Tom;



More information about the Digitalmars-d mailing list