digitalmars.D - const methods()
- Mike (5/5) Jan 22 2007 D doesnt seem to have any way of protecting a class object from being ch...
- Frits van Bommel (6/11) Jan 22 2007 There's not really an alternative line of thought involved, except in
- Andrey Khropov (4/5) Jan 22 2007 Why?
- Frits van Bommel (3/8) Jan 22 2007 Well, that seems to be Walters motivation, at least. Though I might have...
- Mike (2/2) Jan 22 2007 So what should I do until an alternative arrives? Just hope that no one ...
- BCS (17/20) Jan 22 2007 Its really hacky but you might be able to make something like this work
- renoX (3/6) Jan 23 2007 I remember that this is common knowledge, but I don't remember why.
- Mike (3/3) Jan 23 2007 Well even the C++ way worked pretty well. Whatever method is used we nee...
- Kristian Kilpi (28/35) Jan 23 2007 One thing I hate with C++'s const, sometimes, is that it applies to the ...
- Mike (2/2) Jan 23 2007 Kristian, the mutable keyword allows you to override the constness. So i...
- Kristian Kilpi (4/8) Jan 23 2007 Wha!? I can't believe I have missed that one! 8| Hehheh... well, you
- James Dennett (14/23) Jan 23 2007 Without wishing to pick on Kristian, this is fairly common
- Kristian Kilpi (19/42) Jan 23 2007 Thanks (for not picking on me). Actually I have used C++ a lot and done ...
- Bill Baxter (5/13) Jan 23 2007 If you learned C++ pretty early on then it's not too surprising that you...
- Kristian Kilpi (7/21) Jan 24 2007 Well, that solves it; I'm a C++ old schooler.
- Miles (8/10) Jan 23 2007 Me neither.
- Andrei Alexandrescu (See Website For Email) (5/18) Jan 23 2007 C++'s const is not "broken" by a certain definition of broken, but it
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
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
Frits van Bommel wrote:the way C++ does const sucks :P.Why? -- AKhropov
Jan 22 2007
Andrey Khropov wrote:Frits van Bommel wrote:Well, that seems to be Walters motivation, at least. Though I might have overstated it a bit for effect :).the way C++ does const sucks :P.Why?
Jan 22 2007
So what should I do until an alternative arrives? Just hope that no one messes with my sensitive class objects?
Jan 22 2007
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
Andrey Khropov Wrote:Frits van Bommel wrote: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.the way C++ does const sucks :P.Why?
Jan 23 2007
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
On Tue, 23 Jan 2007 12:18:28 +0200, renoX <renosky free.fr> wrote:Andrey Khropov Wrote:=Frits van Bommel wrote: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 =the way C++ does const sucks :P.Why?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
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
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
Kristian Kilpi wrote:On Tue, 23 Jan 2007 14:52:30 +0200, Mike <mike mike.com> wrote: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. -- JamesKristian, 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
On Tue, 23 Jan 2007 18:15:14 +0200, James Dennett <jdennett acm.org> wrote:Kristian Kilpi 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. 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>On Tue, 23 Jan 2007 14:52:30 +0200, Mike <mike mike.com> wrote: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. -- JamesKristian, 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
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
On Wed, 24 Jan 2007 04:52:50 +0200, Bill Baxter <dnewsgroup billbaxter.com> wrote:Kristian Kilpi wrote: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. ;)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 24 2007
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
Miles wrote:Mike wrote: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. AndreiSo 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