Why D const is annoying

Chris Cain clcain at uncg.edu
Wed May 2 13:59:16 PDT 2012


On Wednesday, 2 May 2012 at 20:30:10 UTC, Mehrdad wrote:
> Okay thanks for the replies, that clears up a bunch of things 
> for me.

No problem, I'm glad it's helping.

> Regarding the last part:
>
> Yes, it 'works'. The only trouble is that you can never set the 
> interface methods to be 'const', because any sort of 
> indirection can screw you over later. This is why const is less 
> useful than it could be.

You can set interface methods to be const, but you have to
understand that you're saying that method can't mutate state.
There's also nothing stopping you from specifying that both const
and non-const methods are available, but it would increase the
workload of people down the line especially if the const and
non-const version differ significantly.

I think D's const is as useful as it needs/can be. That said, I
can understand why you might feel that way until you start using
const/immutable correctly. Once you see how it improves your
conceptual model of approaching algorithms, I won't be surprised
if you end up as annoyed with C++'s const as I am. It's kind of 
why I pointed out the whole thing where you should lock your 
object even when you're just calling getters on a const object. 
It's actually deceptively incorrect to not do that in C++.


More information about the Digitalmars-d mailing list