Why D const is annoying
Jonathan M Davis
jmdavisProg at gmx.com
Wed May 2 16:02:07 PDT 2012
On Wednesday, May 02, 2012 13:30:11 Mehrdad wrote:
> Okay thanks for the replies, that clears up a bunch of things for me.
>
> 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_ make the interface methods const if you're willing to deal with that
restriction in everything that implements it. Sometimes that's reasonable.
Sometimes it's not. The simple truth of the matter is that you can't just
const in D as much as you would in C++, simply because it's more restrictive.
It's more powerful thanks to its greater guarantees, but that comes at a cost.
So, programmers are going to have to learn when using const in D makes sense
and when it doesn't, even if they're experts at in C++. And a lot of it
depends on what you're doing (e.g. I pretty much never lazy load anything, so
const would never cause me problems due to disallowing lazy loading, but for
someone who needs lazy loading for performance, const make be unacceptable in
many cases).
- Jonathan M Davis
More information about the Digitalmars-d
mailing list