www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - const methods()

reply Mike <mike mike.com> writes:
D doesnt seem to have any way of protecting a class object from being changed
by non-const functions. For example in C++ I can declare a class reference as
const, so no one can call any methods on that reference that would change the
contents of the object. Why can you not do this in D? What is the alternative
line of thought to this?
Jan 22 2007
next sibling parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Mike wrote:
 D doesnt seem to have any way of protecting a class object from being changed
 by non-const functions. For example in C++ I can declare a class reference as
 const, so no one can call any methods on that reference that would change the
 contents of the object. Why can you not do this in D? What is the alternative
 line of thought to this?
There's not really an alternative line of thought involved, except in the sense that the way C++ does const sucks :P. Active work is currently being done to design a way to do const (and some other things) that is hopefully better. Until then, we'll have to do without :(.
Jan 22 2007
next sibling parent reply "Andrey Khropov" <andkhropov_nosp m_mtu-net.ru> writes:
Frits van Bommel wrote:

 the way C++ does const sucks :P.
Why? -- AKhropov
Jan 22 2007
parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Andrey Khropov wrote:
 Frits van Bommel wrote:
 
 the way C++ does const sucks :P.
Why?
Well, that seems to be Walters motivation, at least. Though I might have overstated it a bit for effect :).
Jan 22 2007
prev sibling parent reply Mike <mike mike.com> writes:
So what should I do until an alternative arrives? Just hope that no one messes
with my sensitive class objects?
Jan 22 2007
parent BCS <ao pathlink.com> writes:
Reply to mike,

 So what should I do until an alternative arrives? Just hope that no
 one messes with my sensitive class objects?
 
Its really hacky but you might be able to make something like this work interface C { int nonConst(); } private ConstC : C { int nonConst(){assert(0);} } private NonConstC : C { int nonConst() {/*go*/} } having a const and non const view might be really messy (nested non private classes maybe?) On second thought, you'd better not
Jan 22 2007
prev sibling parent reply renoX <renosky free.fr> writes:
Andrey Khropov Wrote:
 Frits van Bommel wrote:
 the way C++ does const sucks :P.
Why?
I remember that this is common knowledge, but I don't remember why. If memory serves, this is because in C++ you can 'unconst' parameters with a cast, so the compiler cannot really optimise the code.
Jan 23 2007
next sibling parent Mike <mike mike.com> writes:
Well even the C++ way worked pretty well. Whatever method is used we need a
solution to this problem.

Walter are you going to introduce a fix to this problem?
Jan 23 2007
prev sibling parent reply "Kristian Kilpi" <kjkilpi gmail.com> writes:
On Tue, 23 Jan 2007 12:18:28 +0200, renoX <renosky free.fr> wrote:
 Andrey Khropov Wrote:
 Frits van Bommel wrote:
 the way C++ does const sucks :P.
Why?
I remember that this is common knowledge, but I don't remember why. If memory serves, this is because in C++ you can 'unconst' parameters =
=
 with a cast, so the compiler cannot really optimise the code.
One thing I hate with C++'s const, sometimes, is that it applies to the = = whole class, not just the part that's visible to the user. For example: class MyClass { public: int getVal() const { update_cache(); //(error, not const) return m_cache.val; } protected: void update_cache() { if(m_cache.is_dirty) { m_cache.is_dirty =3D false; m_cache.val =3D ...; } } my_cache_t m_cache; }; I would like to have different 'layers of constness'. E.g. one layer = guarantees that a method doesn't alter the public state of the class, th= e = second layer that the non-public state is not changed. In the example above, one have to do an 'unconst' in 'getVal()': ((MyClass *)this)->update_cache(); When carefully used it's useful, and necessary for 'greater good'. ;)
Jan 23 2007
parent reply Mike <mike mike.com> writes:
Kristian, the mutable keyword allows you to override the constness. So in my
opinion, the way C++ does const objects is perfect. Cant understand why its not
in D.
Jan 23 2007
next sibling parent reply "Kristian Kilpi" <kjkilpi gmail.com> writes:
On Tue, 23 Jan 2007 14:52:30 +0200, Mike <mike mike.com> wrote:
 Kristian, the mutable keyword allows you to override the constness. So  
 in my
 opinion, the way C++ does const objects is perfect. Cant understand why  
 its not in D.
Wha!? I can't believe I have missed that one! 8| Hehheh... well, you learn every day something new (sometimes pretty essential things it seems)... Thanks for pointing this out. :)
Jan 23 2007
parent reply James Dennett <jdennett acm.org> writes:
Kristian Kilpi wrote:
 On Tue, 23 Jan 2007 14:52:30 +0200, Mike <mike mike.com> wrote:
 Kristian, the mutable keyword allows you to override the constness. So
 in my
 opinion, the way C++ does const objects is perfect. Cant understand
 why its not in D.
Wha!? I can't believe I have missed that one! 8| Hehheh... well, you learn every day something new (sometimes pretty essential things it seems)... Thanks for pointing this out. :)
Without wishing to pick on Kristian, this is fairly common of "your language sucks because..." arguments -- those who know the language better know the argument to be based on insufficient understanding, but that doesn't stop it being promulgated enthusiastically. Right now this probably happens more to C and C++ than to any other languages (which isn't to say that those languages don't have plenty of legitimate warts), but if/when D has more success the D community will likely be on the receiving end of this. Learning from how this has hurt other languages could enable the D community to manage the propaganda war more effectively. -- James
Jan 23 2007
parent reply "Kristian Kilpi" <kjkilpi gmail.com> writes:
On Tue, 23 Jan 2007 18:15:14 +0200, James Dennett <jdennett acm.org> wrote:
 Kristian Kilpi wrote:
 On Tue, 23 Jan 2007 14:52:30 +0200, Mike <mike mike.com> wrote:
 Kristian, the mutable keyword allows you to override the constness. So
 in my
 opinion, the way C++ does const objects is perfect. Cant understand
 why its not in D.
Wha!? I can't believe I have missed that one! 8| Hehheh... well, you learn every day something new (sometimes pretty essential things it seems)... Thanks for pointing this out. :)
Without wishing to pick on Kristian, this is fairly common of "your language sucks because..." arguments -- those who know the language better know the argument to be based on insufficient understanding, but that doesn't stop it being promulgated enthusiastically. Right now this probably happens more to C and C++ than to any other languages (which isn't to say that those languages don't have plenty of legitimate warts), but if/when D has more success the D community will likely be on the receiving end of this. Learning from how this has hurt other languages could enable the D community to manage the propaganda war more effectively. -- James
Thanks (for not picking on me). Actually I have used C++ a lot and done preeetty big projects with it. It's amazing that I hadn't noticed the 'mutable' keyword during all these years. When I came across a situation I described earlier in my example, the first thing I thought of was to do a nonconst cast and be done with it. That is, C++ allows you to do that. If nonconst casting had not been possible, then I would have searched for different solution, and noticed that 'mutable' exists. But yeah, partial understanding of a language can cause "your language sucks" remarks. But maybe the first impression of the language wasn't the best possible then. For example, lisp is not for me. I mean, it's a great language with cool features etc, but I have found it not-so-easy to code with. Of course, a couple of years of hard coding with it would probably change that. On the other hand, some features and limitations of a language can begin to annoy you after a while. For instance, I have learnt to *hate* the header files of C and C++. And, of course, if someone says "that sucks" without explaining why is that, nobody should notice that remark. I did explain what was unfortunate with C++ const in my opinion, and I was 'corrected' right away. <g>
Jan 23 2007
parent reply Bill Baxter <dnewsgroup billbaxter.com> writes:
Kristian Kilpi wrote:
 On Tue, 23 Jan 2007 18:15:14 +0200, James Dennett <jdennett acm.org> wrote:
 Thanks (for not picking on me). Actually I have used C++ a lot and done 
 preeetty big projects with it. It's amazing that I hadn't noticed the 
 'mutable' keyword during all these years. When I came across a situation 
 I described earlier in my example, the first thing I thought of was to 
 do a nonconst cast and be done with it. That is, C++ allows you to do 
 that. If nonconst casting had not been possible, then I would have 
 searched for different solution, and noticed that 'mutable' exists.
If you learned C++ pretty early on then it's not too surprising that you weren't aware of 'mutable'. It was a fairly late addition. I think the first copy of Stroustrup I bought did not have 'mutable'. --bb
Jan 23 2007
parent "Kristian Kilpi" <kjkilpi gmail.com> writes:
On Wed, 24 Jan 2007 04:52:50 +0200, Bill Baxter  
<dnewsgroup billbaxter.com> wrote:
 Kristian Kilpi wrote:
 On Tue, 23 Jan 2007 18:15:14 +0200, James Dennett <jdennett acm.org>  
 wrote:
 Thanks (for not picking on me). Actually I have used C++ a lot and done  
 preeetty big projects with it. It's amazing that I hadn't noticed the  
 'mutable' keyword during all these years. When I came across a  
 situation I described earlier in my example, the first thing I thought  
 of was to do a nonconst cast and be done with it. That is, C++ allows  
 you to do that. If nonconst casting had not been possible, then I would  
 have searched for different solution, and noticed that 'mutable' exists.
If you learned C++ pretty early on then it's not too surprising that you weren't aware of 'mutable'. It was a fairly late addition. I think the first copy of Stroustrup I bought did not have 'mutable'. --bb
Well, that solves it; I'm a C++ old schooler. Hmm, I think I should update my C++ knowledge... there might be something else I'm not aware of. Yes, there must be lots of new cool features that'll make coding an absolute breeze... Yeah, and then I want to win lottary. ;)
Jan 24 2007
prev sibling parent reply Miles <_______ _______.____> writes:
Mike wrote:
 So in my
 opinion, the way C++ does const objects is perfect. Cant understand why its
not in D.
Me neither. I read this newsgroup for some years, and have never understood why D doesn't have const members, other than "Walter doesn't like them". There is a teaching that says that we should not try to fix what is not broken. So, IMO, while nobody manages to figure out what is broken in C++'s const, we should just stick with it. It works exactly how it is intended to, and does a good job. Just adopt it.
Jan 23 2007
parent "Andrei Alexandrescu (See Website For Email)" <SeeWebsiteForEmail erdani.org> writes:
Miles wrote:
 Mike wrote:
 So in my
 opinion, the way C++ does const objects is perfect. Cant understand why its
not in D.
Me neither. I read this newsgroup for some years, and have never understood why D doesn't have const members, other than "Walter doesn't like them". There is a teaching that says that we should not try to fix what is not broken. So, IMO, while nobody manages to figure out what is broken in C++'s const, we should just stick with it. It works exactly how it is intended to, and does a good job. Just adopt it.
C++'s const is not "broken" by a certain definition of broken, but it certainly can be vastly improved. I'm not sure how this idea that "nobody" figured out what the issues are with C++'s const came about. Andrei
Jan 23 2007