What's wrong with just a runtime-checked const?
Andrei Khropov
andkhropov at nospam_mtu-net.ru
Sat Jul 15 15:18:28 PDT 2006
Reiner Pope wrote:
> Disclaimer: my background is a small amount of Java and C#, so perhaps I am
> completely confusing the issue because of practically no C++ experience.
> Please tell me if that's the case.
>
> Walter seems to have two main objections to a C++-style const:
> 1. it can be subverted with a const_cast, removing any guarantees about it.
Different C++ style casts are not supported in D so it's not an issue, I think.
> 2. it is hard to implement, but this doesn't seem to concern him as much as
> the former.
>
> People have expressed a desire for const especially to ensure the
> Copy-on-Write idiom, and to make libraries more robust.
>
> Wouldn't a runtime const check be much more flexible than a compile-time
> check? Const-safeness is fundamentally a correctness-checking feature, just
> like unit tests, so why not make it operate exactly like unit tests?
I think things that can be checked at compile-time should be checked at
compile-time and don't slow down runtime code.
Your proposed type of checks also has a kind of dynamic flavor
(Smalltalk/Python like) which is not very D way :-)
Unit tests are different. They are rather "post-compile" or "pre-runtime" in
fact.
I suppose you actually mean DbC, right?
> The even better thing about this is that most code doesn't need to have
> const-correctness in mind when writing it
I think that const-correct code is important and expresses good design and also
aids compiler in optimization.
-------------------------------------------------------------------------------
Anyway, I think const discussion should be postponed until we get all the
things with imports right which is the highest priority for now, IMHO.
More information about the Digitalmars-d-learn
mailing list