Inherited const when you need to mutate

Era Scarecrow rtcvb32 at yahoo.com
Mon Jul 9 19:43:03 PDT 2012


On Tuesday, 10 July 2012 at 01:41:29 UTC, bearophile wrote:
> David Piepgrass:
>
>> This use case is pretty complex, so if I port this to D, I'd 
>> probably just cast away const/immutable where necessary.
>
> You are not the first person that says similar things. So D 
> docs need to stress more than casting away const/immutable in D 
> is rather more dangerous than doing the same thing in C++.

  Depends on what you are trying to do I suppose. The only 
mutation I would use is a new object (and cast away const to do a 
bulk copy which is otherwise annoying to do). If something is 
const(ant), it means you don't intend to change it. Perhaps this 
is a bad example:

  Let's say a class/struct is a book with Page protectors 
signifying 'const(ant)'. You promise to return the book to the 
library without making any changes; Although you promised you 
wouldn't make changes, you still take the Page protectors off and 
make make notes on the outer edges or make adjustments in the 
text, then return the book.

  Is this wise? This isn't C++. If something shouldn't change, 
then don't change it god damn it. If it needs to change it isn't 
const(ant) and shouldn't suggest it is. If the functions are 
const(ant) that you need to use, make non-const(ant) versions (or 
ensure the original(s) are unmodified) and/or disable the 
original ones (if you can/need, I think?).

  Seriously, it's not that hard a concept. I guess if something 
doesn't port well from C++ then redesign it. Some things done in 
C++ are hacks due to the language's limitations and faults.

  I hope I'm not rambling too much.


More information about the Digitalmars-d mailing list